Dave2711 Posted July 25, 2009 Share Posted July 25, 2009 Hi, im trying to make an image upload script which will rename the images to something unique to my uploads folder, and post the link of the image just below on the page.. A friend of mine sent me their image upload script which ive editted slightly but still cannot figure out how to force name changes for each file, and I have a feeling he deleted aload of stuff out of it but left some there so its a fairly messy script as it is.. Any chance anyone could help? The script is as follows: <?php //Max file size (Current 6.3mb) $MAX_SIZE = 6300000; //Allow these extensions $FILE_EXTS = array('.jpg','.png','.gif'); //Allow file delete? no, if only allow upload only $DELETABLE = false; $site_name = $_SERVER['HTTP_HOST']; $url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']); $url_this = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; $upload_dir = "uploads/"; $upload_url = $url_dir."/uploads/"; $message =""; /************************************************************ * Process User's Request ************************************************************/ if ($_FILES['userfile']) { $resource = fopen("log.txt","a"); fwrite($resource,date("Ymd h:i:s")." - $_SERVER[REMOTE_ADDR] - " .$_FILES['userfile']['name']." " .$_FILES['userfile']['type']."\n"); fclose($resource); $file_type = $_FILES['userfile']['type']; $file_name = $_FILES['userfile']['name']; $file_ext = strtolower(substr($file_name,strrpos($file_name,"."))); //File Size Check if ( $_FILES['userfile']['size'] > $MAX_SIZE) $message = "The file size is over 6MB."; //File Extension Check else if (!in_array($file_ext, $FILE_EXTS)) $message = "$file_name($file_type) is not a valid format."; else $message = do_upload($upload_dir, $upload_url); print "<script>window.location.href='$url_this?message=$message'</script>"; } else if (!$_FILES['userfile']); else $message = "Specified file does not exist, or is incorrect format."; $handle=opendir($upload_dir); $filelist = ""; while ($file = readdir($handle)) { if(!is_dir($file) && !is_link($file)) { $filelist .= "<a href='$upload_dir$file'>".$file."</a> - URL: <b>$upload_url$file</b>"; if ($DELETABLE) $filelist .= " Added at ".date("d-m H:i", filemtime($upload_dir.$file)) .""; $filelist .= " <a style='text-decoration:none; font-weight:bold' href='?del=$upload_dir".urlencode($file)."' title='delete'>x</a>"; $filelist .="<br>"; } } function do_upload($upload_dir, $upload_url) { $temp_name = $_FILES['userfile']['tmp_name']; $file_name = $_FILES['userfile']['name']; $file_name = str_replace("\\","",$file_name); $file_name = str_replace("'","",$file_name); $file_path = $upload_dir.$file_name; //File Name Check if ( $file_name =="") { $message = "Invalid File Name Specified"; return $message; } $result = move_uploaded_file($temp_name, $file_path); if (!chmod($file_path,0777)) $message = "Destination Folder does not exist, please contact Administator."; else $message = ($result)?"$file_name was uploaded successfully.<br>Direct URL: http://bla.com/$file_name" : "Upload Failed."; return $message; } ?> <html> <head> <title>Page Title</title> <link rel=stylesheet href="style.css"> </head> <body> <br><br> <center> <br> Free <b>.JPG</b>, <b>.GIF</b>, <b>.PNG</b> file hosting. <br> <form name="upload" id="upload" ENCTYPE="multipart/form-data" method="post"> Upload File: <input type="file" id="userfile" name="userfile"> <input type="submit" name="upload" value="Upload"> </form> <br> <b><u>Upload Details:</b></u> <br><br> <font color=red><?=$_REQUEST[message]?></font> </center> If anyone can help tidy some of it up ill be grateful too.. as I was hoping to learn something from it but as it is I dont understand half of it. Thanks very much, Dave Quote Link to comment https://forums.phpfreaks.com/topic/167395-image-uploadrename/ Share on other sites More sharing options...
ldougherty Posted July 25, 2009 Share Posted July 25, 2009 You should read through a file upload tutorial to understand exactly how it works. Once you have the concept you'll see that renaming the file to whatever you want is rather simple. http://www.tizag.com/phpT/fileupload.php Quote Link to comment https://forums.phpfreaks.com/topic/167395-image-uploadrename/#findComment-882674 Share on other sites More sharing options...
Dave2711 Posted July 25, 2009 Author Share Posted July 25, 2009 Ah brilliant thanks, I suppose actually learning the script rather than stealing his is more logical as I do want to tamper with it quite a bit.. Thanks very much Dave Quote Link to comment https://forums.phpfreaks.com/topic/167395-image-uploadrename/#findComment-882679 Share on other sites More sharing options...
.josh Posted July 25, 2009 Share Posted July 25, 2009 Quote Link to comment https://forums.phpfreaks.com/topic/167395-image-uploadrename/#findComment-882688 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.