JamesMore Posted September 26, 2014 Share Posted September 26, 2014 Hello, Its my first day coding so please forgive me for some of the silly errors I might make. I am writing a small upload script and I want to have some sort of orginazation by uploading images to a direcotry based on the day.The following is my code but it will fail to save the file Warning: file_put_contents(images/14-09-26/5425905a18bba.png): failed to open stream: No such file or directory This is becuase the directory with the data part is not created how should I go about first checking that this directory is there and then creating it ? <?php ini_set('display_errors',1); error_reporting(E_ALL); $today = date('y-m-j'); define('UPLOAD_DIR', 'images/' .$today. '/'); $img = $_POST['data']; $img = str_replace('data:image/png;base64,', '', $img); $img = str_replace(' ', '+', $img); $data = base64_decode($img); $file = UPLOAD_DIR . uniqid() . '.png'; $success = file_put_contents($file, $data); print $success ? $file : 'Unable to save the file.'; ?> Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted September 26, 2014 Share Posted September 26, 2014 (edited) You need to create the parent directories first before creating the file. You'd use mkdir to do this by setting the $recursive flag to true. Your method of uploading the file is odd. Why is it being uploaded in base64 encoding? You can upload files by using a file input field and setting the forms enctype to multipart/form-data, eg <form action="upload.php" method="post"> File to upload: <input type="file" name="file" /> <input type="submit" value="Upload" /> </form> You can then retrieve the uploaded file using the $_FILES superglobal array. See this manual page for more information on how PHP handles file uploads. Edited September 26, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
JamesMore Posted September 26, 2014 Author Share Posted September 26, 2014 Thanks for the fast answer I will try and see if I can use the mkdir option The reason for the upload files in base64 is becuase the images are being posted from in base64 from the html2canvas.js libaray Quote Link to comment Share on other sites More sharing options...
CroNiX Posted September 26, 2014 Share Posted September 26, 2014 define('UPLOAD_DIR', 'images/' .$today. '/'); //Check that the dir exists if ( ! file_exists(UPLOAD_DIR)) { //Nope, create it... mkdir(UPLOAD_DIR); } //rest of upload code to move uploaded file to UPLOAD_DIR Quote Link to comment Share on other sites More sharing options...
JamesMore Posted September 26, 2014 Author Share Posted September 26, 2014 define('UPLOAD_DIR', 'images/' .$today. '/'); //Check that the dir exists if ( ! file_exists(UPLOAD_DIR)) { //Nope, create it... mkdir(UPLOAD_DIR); } //rest of upload code to move uploaded file to UPLOAD_DIR Your code worked I had also just found is_dir and it seemed to do the job as well sould i use file_exists over is_dir ? define('UPLOAD_DIR', 'images/' .$today. '/'); if (!is_dir(UPLOAD_DIR)) { mkdir(UPLOAD_DIR); } Quote Link to comment Share on other sites More sharing options...
JamesMore Posted September 26, 2014 Author Share Posted September 26, 2014 (edited) Here is my full code so far, I am using is_dir to check for the directory and then create it , I have commented the code so its easy to see what I am trying to do.There seems to be a bug in the print statment The file is uploaded to images/14-09-26/5425a625da30f.png And i want to return the path with out the extension but I am just now getting the file name and not the path 5425a625da30f <?php // report all errors for debug ini_set('display_errors',1); error_reporting(E_ALL); // Get today's date so we can upload images to a directory with a date $today = date('y-m-j'); define('UPLOAD_DIR', 'images/' .$today. '/'); // Check if today's directory is there if not create if (!is_dir(UPLOAD_DIR)) { mkdir(UPLOAD_DIR); } // Handle the base64 upload and file naming $img = $_POST['data']; $img = str_replace('data:image/png;base64,', '', $img); $img = str_replace(' ', '+', $img); $data = base64_decode($img); $file = UPLOAD_DIR . uniqid() . '.png'; $success = file_put_contents($file, $data); // get the filename without the extension $info = pathinfo($file); $image_name = basename($file,'.'.$info['extension']); // Print the Status needs to be in the following format path/name||randomreference eg images/2014-09-26/b-xlxlm-q9kg8||ox6ztm print $success ? $image_name : 'Unable to save the file.'; ?> Edited September 26, 2014 by JamesMore Quote Link to comment Share on other sites More sharing options...
CroNiX Posted September 26, 2014 Share Posted September 26, 2014 Your code worked I had also just found is_dir and it seemed to do the job as well sould i use file_exists over is_dir ? define('UPLOAD_DIR', 'images/' .$today. '/'); if (!is_dir(UPLOAD_DIR)) { mkdir(UPLOAD_DIR); } You can use either. file_exists() works on both files and dirs and I usually use it for both cases. But yeah, is_dir() would obviously work for this. To get the filename/path without extension, why don't you just grab it before you add the extension? $raw_file = UPLOAD_DIR . uniqid(); $file = $raw_file . '.png'; ... print $success ? $raw_file : 'Unable to save the file.'; Quote Link to comment Share on other sites More sharing options...
JamesMore Posted September 28, 2014 Author Share Posted September 28, 2014 To get the filename/path without extension, why don't you just grab it before you add the extension? $raw_file = UPLOAD_DIR . uniqid(); $file = $raw_file . '.png'; ... print $success ? $raw_file : 'Unable to save the file.'; Thanks croNix seems with code there is aways a 100 different ways of doing thing but your way seems the best. I have updated my code and now written the last bit of the puzzle to create a random reference number, I did read that the random method I am using is not perfect but not sure give how its being used that I am not that worried (but if yo know of a better method please post) Question is about security as i am allowing write access with this script should I add some sort of check to make sure the file is infact a png file ? <?php // report all errors for debug ini_set('display_errors',1); error_reporting(E_ALL); // Get today's date so we can upload images to a directory with a date $today = date('Y-m-j'); define('UPLOAD_DIR', 'images/' .$today. '/'); // Check if today's directory is there if not create if (!is_dir(UPLOAD_DIR)) { mkdir(UPLOAD_DIR); } // Handle the base64 upload and file naming $img = $_POST['data']; $img = str_replace('data:image/png;base64,', '', $img); $img = str_replace(' ', '+', $img); $data = base64_decode($img); $raw_file = UPLOAD_DIR . uniqid(); $file = $raw_file . '.png'; $success = file_put_contents($file, $data); // Create a semi random reference string $Ref = substr(md5(rand()),0,6); // Print the Status and return a reference id print $success ? $raw_file . "||" . $Ref : 'Unable to save the file.'; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.