Noctorum Posted August 9, 2004 Share Posted August 9, 2004 ><?php $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 = "upload_files/"; $upload_url = $url_dir."/upload_files/"; $message =""; //create upload_files directory if not exist //If it does not work, create on your own and change permission. if (!is_dir("upload_files")) { die ("upload_files directory doesn't exist"); } if ($_FILES['userfile']) { $message = do_upload($upload_dir, $upload_url); } else { $message = "Invalid File Specified."; } print $message; function do_upload($upload_dir, $upload_url) { $temp_name = $_FILES['userfile']['tmp_name']; $file_name = $_FILES['userfile']['name']; $file_type = $_FILES['userfile']['type']; $file_size = $_FILES['userfile']['size']; $result = $_FILES['userfile']['error']; $file_url = $upload_url.$file_name; $file_path = $upload_dir.$file_name; //File Name Check if ( $file_name =="") { $message = "Invalid File Name Specified"; return $message; } //File Size Check else if ( $file_size > 300000) { $message = "The file size is over 300K."; return $message; } //File Type Check else if ( $file_type == "text/plain" ) { $message = "Sorry, You cannot upload any script file"; return $message; } $result = move_uploaded_file($temp_name, $file_path); $message = ($result)?"File url <a href=$file_url>$file_url</a>" : "Somthing is wrong with uploading a file."; return $message; } ?> This is what I got so far from hotscripts. I'd like to add in a check to make sure that a file can't be overwritten if two files that are uploaded have the same name. How could I add that? Thanks Quote Link to comment Share on other sites More sharing options...
borfast Posted August 10, 2004 Share Posted August 10, 2004 The onyl way I can think of is to use a timestamp to make the filename unique. So instead of having $file_name = $_FILES['userfile']['name']; you could have $file_name = time()."_".$_FILES['userfile']['name']; Also, I'm moving the thread to a more appropriate forum, for better organization. Quote Link to comment Share on other sites More sharing options...
snipe Posted August 11, 2004 Share Posted August 11, 2004 Well, you could also use file_exists() to first check and see if the filename exists, and then stick something on the end of of the filename to make it different. An example of the logic would be: 1. check if the filename already exists using file_exists() 2. if it does, split the filename, getting the base filename and the extension (using explode() or something else) 3. use something like date("U") to get the current unix time and tack that onto the base filename Make sense? Quote Link to comment Share on other sites More sharing options...
snipe Posted August 11, 2004 Share Posted August 11, 2004 Or of course you could just stop the script at the file_exists() point and return an erro tellintg the user to rename their file. Quote Link to comment Share on other sites More sharing options...
matman Posted August 11, 2004 Share Posted August 11, 2004 For large-scale file upload and storage, you've got to index the files in a database. Store the original filename in the database, but use the autonumber index to name the file you actually store on your webserver. That will GUARANTEE filename uniqueness. 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.