barrowvian Posted March 28, 2010 Share Posted March 28, 2010 so far i have; // Configuration - Your Options $allowed_filetypes = array('.jpg','.gif','.bmp','.png'); // These will be the types of file that will pass the validation. $max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB). $upload_path = $_COOKIE['tmp']; // upload files to. called from cookie tmp from newuser.php $filename = $_FILES['userfile1']['name']; // Get the name of the file (including file extension). $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename. // Check if the filetype is allowed, if not DIE and inform the user. if(!in_array($ext,$allowed_filetypes)) die('The file you attempted to upload is not allowed.'); // Check if we can upload to the specified path, if not DIE and inform the user. if(!is_writable($upload_path)) die('You cannot upload to the specified directory, please CHMOD it to 777.'); // Now check the filesize, if it is too large then DIE and inform the user. if(filesize($_FILES['userfile1']['tmp_name']) > $max_filesize) die('The file you attempted to upload is too large.'); // Upload the file to your specified path. if(move_uploaded_file($_FILES['userfile1']['tmp_name'],$upload_path . $filename)) echo 'Upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a><br />'; else echo 'There was an error during the file upload. Please try again.'; Ive just been playing around with uploading images etc and have the basics working. Now I'd like to rename this file 'profile' and use its original extension. I know the rename function will be used here as Ive been looking around online for what Im looking for but I havent been able to successfully implement it yet. Please could someone help me out, thanks Link to comment https://forums.phpfreaks.com/topic/196820-renaming-a-uploaded-file/ Share on other sites More sharing options...
Kieran Menor Posted March 28, 2010 Share Posted March 28, 2010 move_uploaded_file() is basically a renaming function (but it does a few other things too), so instead of move_uploaded_file($_FILES['userfile1']['tmp_name'],$upload_path . $filename) You could do $pathinfo = pathinfo($_FILES['userfile1']['name']); move_uploaded_file($_FILES['userfile1']['tmp_name'],$upload_path . 'profile.' . $pathinfo['extension']) Link to comment https://forums.phpfreaks.com/topic/196820-renaming-a-uploaded-file/#findComment-1033226 Share on other sites More sharing options...
barrowvian Posted March 28, 2010 Author Share Posted March 28, 2010 ah right, i didnt realise that move_uploaded_file() could be used to rename the file too. i implemented your advice and it worked perfectly and has made displaying the images so much easier, thank you Link to comment https://forums.phpfreaks.com/topic/196820-renaming-a-uploaded-file/#findComment-1033231 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.