coolphpdude Posted October 24, 2007 Share Posted October 24, 2007 hey guys, right, i need help picking apart the upload of a file. I'm at the point where i can upload the file to where i want it to go. what i want/need to make sure is that when the user uploads an image it is resized (i will do this later) and if the filename already exists then it is renamed. I currently have the following code: if ((($_FILES["uploadedfile"]["type"] == "image/gif") || ($_FILES["uploadedfile"]["type"] == "image/jpeg") || ($_FILES["uploadedfile"]["type"] == "image/pjpeg") || ($_FILES["uploadedfile"]["type"] == "image/png") || ($_FILES["uploadedfile"]["type"] == "image/bmp")) && ($_FILES["uploadedfile"]["size"] < 2000000)) { // Where the file is going to be placed $target_path = "uploads/$userid/$classid/"; /* Add the original filename to our target path. Result is "uploads/filename.extension" */ $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); $_FILES['uploadedfile']['tmp_name']; echo "$target_path"; echo "<p>"; if (file_exists($target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name'])." exists"; echo "<p>"; // make the random file name $randname = md5(rand() * time()); echo "$randname"; $target_path = $target_path . $randname; echo "<p>"; echo "$target_path"; } else { if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name'])." has been uploaded"; } else { echo "There was an error uploading the file, please try again!"; } } } else { echo "invalid file type"; } ...This works ok to upload it to the location i want it to go but i problem is with pulling the filename apart to rename it if it already exists. Any help? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/74596-image-upload/ Share on other sites More sharing options...
coolphpdude Posted October 24, 2007 Author Share Posted October 24, 2007 At the moment its adding the randomly generated code after the file extension Quote Link to comment https://forums.phpfreaks.com/topic/74596-image-upload/#findComment-376990 Share on other sites More sharing options...
kernelgpf Posted October 24, 2007 Share Posted October 24, 2007 Edit this code to fit your script: $v=$_SERVER["DOCUMENT_ROOT"]; $file=$_FILES["file"]["name"]; $gfileplace="$v/gallery/$file"; if(file_exists($gfileplace) == "1") { echo 'Sorry! This file already exists. '; exit; } Quote Link to comment https://forums.phpfreaks.com/topic/74596-image-upload/#findComment-377113 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.