Handling a file passed as a POST request
Category: Files
Remote files
Copy a remote file to local server
Working with files
Delete File Copy File
Working with directories
Create directory
Temporary File
Creating a temporary file //Create temporary file destination if (!is_dir('temp/')) { mkdir('temp', 0777, true); } $decrypted_filename = 'temp/' . uniqid(rand(), true) . '.txt'; //Use it… //Delete the file unlink($decrypted_filename);
Write File
Write text to a file $output_text = "Hello"; file_put_contents("files/output_filename.txt", $output_text); //(Will overwrite an existing file) Note that the directory you write the file to will need chomd set to 777 (php needs the X bit set to be able to write). This can be a potential security risk if this is a concern.
Read file
Read a text file Read a binary file to an array Your file may be byte based by PHP works to stop you operating at a raw byte level, so you have to workaround that a bit by using unpack If your file is very big then get clever with fread etc and only read chunks […]
Paths
Up 1 parent directory level PHP Website Root directory This will give you the root directory of the current website (not the server root), e.g. ‘/public_html/’. If you want to access a folder above it you can do this: The current file Filesystem path – web server referenced
Importing MySQL Table From An Uploaded CSV File
Loading A Table From An Uploaded .csv File <?php include_once('config.php'); //—– CONNECT TO DATABASE —– mysql_connect("localhost", $songs_db_username, $songs_db_password, $songs_db_name) or die("cannot connect"); mysql_select_db($songs_db_name) or die(mysql_error()); //Select the database so it doesn't need to be specified in mysql functions if ( (isset($_POST[‘action’])) && ($_POST[‘action’] == "uploading_all_songs_file") ) { //—————————————- //—– UPLOADING NEW ALL SONGS FILE —– […]
echo a File
Echo'ing the contents of a file //Get the url of a file and echo that file – this can be a remote web site file or a local one if (isset($_POST['url'])) { $url_string = $_POST['url']; $url_string = stripslashes(htmlentities(strip_tags($url_string))); echo file_get_contents("http://$url_string"); }