Jump to content

JamesMore

New Members
  • Posts

    5
  • Joined

  • Last visited

JamesMore's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. 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.'; ?>
  2. 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.'; ?>
  3. 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); }
  4. 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
  5. 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.'; ?>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.