Jump to content

[SOLVED] Modify this Code


.Darkman

Recommended Posts

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,

Link to comment
https://forums.phpfreaks.com/topic/46716-solved-modify-this-code/
Share on other sites

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.

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

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

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'];

?> 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.