Jump to content

Any Php Scripters In The House? :x


Noctorum
 Share

Recommended Posts

><?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? :lol:

 

Thanks :P

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...