.Darkman Posted April 12, 2007 Share Posted April 12, 2007 Hello Everybody, I have a code to upload image to the server. <?php echo('Upload images: <FORM ENCTYPE="multipart/form-data" ACTION="'.$_SERVER['PHP_SELF'].'" METHOD="POST"> The file: <INPUT TYPE="file" NAME="userfile"> <INPUT TYPE="submit" VALUE="Upload"> </FORM>'); $path = "C:\Program Files\EasyPHP1-8\www\ctut\image\\"; $max_size = 200000; if (!isset($HTTP_POST_FILES['userfile'])) exit; if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) { if ($HTTP_POST_FILES['userfile']['size']>$max_size) { echo "The file is too big<br>n"; exit; } if (($HTTP_POST_FILES['userfile']['type']=="image/gif") || ($HTTP_POST_FILES['userfile']['type']=="image/pjpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/jpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/png")) { if (file_exists($path . $HTTP_POST_FILES['userfile']['name'])) { echo "The file already exists<br>n"; exit; } $res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path . $HTTP_POST_FILES['userfile']['name']); if (!$res) { echo "upload failed!<br>n"; exit; } else { echo "upload sucessful<br>n"; } echo "File Name: ".$HTTP_POST_FILES['userfile']['name']."<br>n"; echo "File Size: ".$HTTP_POST_FILES['userfile']['size']." bytes<br>n"; echo "File Type: ".$HTTP_POST_FILES['userfile']['type']."<br>n"; } else { echo "Wrong file type<br>n"; exit; } } $my_file = $HTTP_POST_FILES['userfile']['name']; ?> But this script does not upload the file if another file with the same name exists. Could some one modify this one to make it such that if the image is present, it is renamed into something like imagename_01.jpg etc.. Please Help Thanks, Quote Link to comment Share on other sites More sharing options...
trq Posted April 12, 2007 Share Posted April 12, 2007 Take a look at the file_exists and rename functions. Quote Link to comment Share on other sites More sharing options...
jitesh Posted April 12, 2007 Share Posted April 12, 2007 Its a better way to give unique name identically I mean if you have a table image in which feilds are id(primarykey),name then keep name like this image_01(id of the record).jpg or you can give name of the file based on time for example $filename = "img_".time().".jpg"; Then you don't need to check file exists. Quote Link to comment Share on other sites More sharing options...
only one Posted April 12, 2007 Share Posted April 12, 2007 use a while loop while(file_exists(the path)){ $userfile = $userfile+1; } or something like that Quote Link to comment Share on other sites More sharing options...
Glyde Posted April 12, 2007 Share Posted April 12, 2007 Using a timestamp will ensure that pretty much, no two files will be the same. Add this before you copy the file $ext = "." . end(explode(chr(46), $HTTP_POST_FILES['userfile']['name'])); $fileName = substr($HTTP_POST_FILES['userfile']['name'], (strlen($ext) * -1)); $savedFileName = $fileName . "_" . time() . $ext; $i = 0; while (file_exists($savedFileName)) { $savedFileName = $fileName . "_" . time() . "_$i" . $ext; $i++; } Then $saveFileName is the filename it should save as. If the user uploads image.jpg, $saveFileName should be something like: image_timestamp.jpg Quote Link to comment Share on other sites More sharing options...
.Darkman Posted April 12, 2007 Author Share Posted April 12, 2007 Using a timestamp will ensure that pretty much, no two files will be the same. Add this before you copy the file $ext = "." . end(explode(chr(46), $HTTP_POST_FILES['userfile']['name'])); $fileName = substr($HTTP_POST_FILES['userfile']['name'], (strlen($ext) * -1)); $savedFileName = $fileName . "_" . time() . $ext; $i = 0; while (file_exists($savedFileName)) { $savedFileName = $fileName . "_" . time() . "_$i" . $ext; $i++; } Then $saveFileName is the filename it should save as. If the user uploads image.jpg, $saveFileName should be something like: image_timestamp.jpg I tried this. Will this work only on my server. Coz it didn't work on my Localhost, powered by EasyPHP Quote Link to comment Share on other sites More sharing options...
Glyde Posted April 12, 2007 Share Posted April 12, 2007 I can't really help if you just tell me it doesn't work. You are aware that that goes BEFORE you copy the file, and then savedFileName is what you put in the copy function, correct. So the final result is something like: <?php echo('Upload images: <FORM ENCTYPE="multipart/form-data" ACTION="'.$_SERVER['PHP_SELF'].'" METHOD="POST"> The file: <INPUT TYPE="file" NAME="userfile"> <INPUT TYPE="submit" VALUE="Upload"> </FORM>'); $path = "C:\Program Files\EasyPHP1-8\www\ctut\image\\"; $max_size = 200000; if (!isset($HTTP_POST_FILES['userfile'])) exit; if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) { if ($HTTP_POST_FILES['userfile']['size']>$max_size) { echo "The file is too big<br>n"; exit; } if (($HTTP_POST_FILES['userfile']['type']=="image/gif") || ($HTTP_POST_FILES['userfile']['type']=="image/pjpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/jpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/png")) { $ext = "." . end(explode(chr(46), $HTTP_POST_FILES['userfile']['name'])); $fileName = substr($HTTP_POST_FILES['userfile']['name'], (strlen($ext) * -1)); $savedFileName = $fileName . "_" . time() . $ext; $i = 0; while (file_exists($savedFileName)) { $savedFileName = $fileName . "_" . time() . "_$i" . $ext; $i++; } $res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path . $savedFileName); if (!$res) { echo "upload failed!<br>n"; exit; } else { echo "upload sucessful<br>n"; } echo "File Name: ".$HTTP_POST_FILES['userfile']['name']."<br>n"; echo "File Size: ".$HTTP_POST_FILES['userfile']['size']." bytes<br>n"; echo "File Type: ".$HTTP_POST_FILES['userfile']['type']."<br>n"; } else { echo "Wrong file type<br>n"; exit; } } $my_file = $HTTP_POST_FILES['userfile']['name']; ?> Quote Link to comment Share on other sites More sharing options...
.Darkman Posted April 12, 2007 Author Share Posted April 12, 2007 Thanks. That worked. But it seems that there is a small error. My files get renamed as .jpg_1176386781.jpg instead of filename__1176386781.jpg ? ? ? Quote Link to comment Share on other sites More sharing options...
only one Posted April 12, 2007 Share Posted April 12, 2007 he means: $userfile = "img_".time().".jpg"; Quote Link to comment Share on other sites More sharing options...
.Darkman Posted April 12, 2007 Author Share Posted April 12, 2007 As you were just posting it, i made it to work. Seems that the multiplication by -1 didn't go thru. So i used this : $fileName = substr($HTTP_POST_FILES['userfile']['name'], 0, (strlen($HTTP_POST_FILES['userfile']['name']) - strlen($ext))); Thanks to everybody who replied. Quote Link to comment Share on other sites More sharing options...
jitesh Posted April 13, 2007 Share Posted April 13, 2007 $filetype = $_FILES['name_of_compenent']['type']; $userfile = "img_".time()."."$filetype; Quote Link to comment Share on other sites More sharing options...
.Darkman Posted April 13, 2007 Author Share Posted April 13, 2007 $filetype = $_FILES['name_of_compenent']['type']; $userfile = "img_".time()."."$filetype; What is that for ? i've already got this to work Quote Link to comment Share on other sites More sharing options...
jitesh Posted April 13, 2007 Share Posted April 13, 2007 This is just to get file type and concate it.... Quote Link to comment Share on other sites More sharing options...
.Darkman Posted April 13, 2007 Author Share Posted April 13, 2007 Oh ! Thanks, Anyway, i got it to work in some other way 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.