Page 1 of 1

Renaming uploaded images

Posted: Wed May 25, 2005 9:52 pm
by tristanlee85
What I am trying to do is once a user has uploaded an image to the server using the PHP form, I want it to display a link to the image that was just uploaded. In my code, i have that, but what I am trying to do is automatically add a %20 in replace of every space that is in the filename.

For example, if the user uploads the file 'photo 1.jpg', I want the URL to be displayed as http://www.server.com/photo%201.jpg. Here is my code.

Code: Select all

<html>
<body bgcolor="black" text="white" link="red" alink="red" vlink="red">

<FORM ENCTYPE="multipart/form-data" ACTION="images.php" METHOD="POST">
To upload a file, click 'Browse' to navigate your local hard drive. Only GIF and JPEG file types are supported.<br><br> <INPUT TYPE="file" NAME="userfile">
<INPUT TYPE="submit" VALUE="Upload">
</FORM>
<p>To view the files currently in the directory, <a href="http://www.plastikracing.net/images/TSN/">click here</a>.
</body>
</html>
<?php

$path = "";
$max_size = 500000000;

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. Files may not exceed 200 KB.<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/JPEG")) {

if (file_exists($path . $HTTP_POST_FILES['userfile']['name'])) { echo "The filename already exists. Please choose a different image or rename the image.<br>\n"; exit; }

$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path .
$HTTP_POST_FILES['userfile']['name']);
if (!$res) { echo "Your file upload has failed!<br>\n"; exit; } else { echo "Your file has been uploaded successfully!<br>\n"; }
echo "<br><br>To link to your file, please copy the following address:<br><br>";
echo "<a href=\"http://www.plastikracing.net/images/TSN/".$HTTP_POST_FILES['userfile']['name']."\">http://www.plastikracing.net/images/TSN/".$HTTP_POST_FILES['userfile']['name']."</a><br>\n";
} else { echo "<br><br> <font color=\"yellow\">Wrong file type. Only GIF and JPEG file types are supported.</font><br>\n"; exit; }

}

?>
Go to http://www.plastikracing.net/images/TSN/images.php to view the page to understand what I am talking about. Feel free to upload a image with a space in the filename to see what I am talking about.

Posted: Wed May 25, 2005 10:35 pm
by hongco
instead of adding %20, you can get rid of the blank by doing this:

change this line:

Code: Select all

$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path .
$HTTP_POST_FILES['userfile']['name']);
to this:

Code: Select all

$newfilename = str_replace(" ", "",$HTTP_POST_FILES['userfile']['name']);
$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path .
$newfilename);
this will change file from: photo 1.jpg to: photo1.jpg without any space

Posted: Wed May 25, 2005 10:57 pm
by tristanlee85
Thank you. Worked perfectly. Now, in this line:

Code: Select all

echo "<a href=\"http://www.plastikracing.net/images/TSN/".$HTTP_POST_FILES['userfile']['name']."\">http://www.plastikracing.net/images/TSN/".$HTTP_POST_FILES['userfile']['name']."</a><br>\n";
the image that is uploaded is changed from 'photo 1' to 'photo1' to the server, but it still displays as 'photo 1' in the URL to view the picture. Do I replace "userfile" in that line to "newfilename"?

Posted: Wed May 25, 2005 11:02 pm
by Burrito
rawurlencode()

edit: posted this as an alternative to hongco's solution I did(as you were posting), help you on the display it won't.

Posted: Wed May 25, 2005 11:11 pm
by tristanlee85
tristanlee85 wrote:Thank you. Worked perfectly. Now, in this line:

Code: Select all

echo "<a href="http://www.plastikracing.net/images/TSN/".$HTTP_POST_FILES['userfile']['name']."">http://www.plastikracing.net/images/TSN/".$HTTP_POST_FILES['userfile']['name']."</a><br>\n";
the image that is uploaded is changed from 'photo 1' to 'photo1' to the server, but it still displays as 'photo 1' in the URL to view the picture. Do I replace "userfile" in that line to "newfilename"?
Ok. That didn't work. I should have known not to ask that question. It's now obvious as wy it wouldn't work. Now, I still haven't figured it out yet though.

Posted: Thu May 26, 2005 12:06 am
by hongco
tristanlee85 wrote:Thank you. Worked perfectly. Now, in this line:

Code: Select all

echo "<a href="http://www.plastikracing.net/images/TSN/".$HTTP_POST_FILES['userfile']['name']."">http://www.plastikracing.net/images/TSN/".$HTTP_POST_FILES['userfile']['name']."</a><br>\n";
the image that is uploaded is changed from 'photo 1' to 'photo1' to the server, but it still displays as 'photo 1' in the URL to view the picture. Do I replace "userfile" in that line to "newfilename"?
that line won't work because the filename has been saved to your site with the name = $newfilename, so use $newfilename.

also take a look at Burrito's post for a different approach to dealing with file names with space:

Posted: Thu May 26, 2005 12:45 am
by tristanlee85
Ok. I read over the link, but I'm a bit lost. This is what I tried, but I'm sure it is wrong.

Code: Select all

echo "<a href=\"http://www.plastikracing.net/images/TSN/", rawurlencode(.$HTTP_POST_FILES['userfile']['name'].)"\">http://www.plastikracing.net/images/TSN/", rawurlencode(.$HTTP_POST_FILES['userfile']['name'].)"</a><br>\n";

Posted: Thu May 26, 2005 12:49 am
by hongco
tristanlee85 wrote:Ok. I read over the link, but I'm a bit lost. This is what I tried, but I'm sure it is wrong.

Code: Select all

echo "<a href="http://www.plastikracing.net/images/TSN/", rawurlencode(.$HTTP_POST_FILES['userfile']['name'].)"">http://www.plastikracing.net/images/TSN/", rawurlencode(.$HTTP_POST_FILES['userfile']['name'].)"</a><br>\n";
rawurlencode will help you replace the space with 20%. That's what Burrito tried to tell you. :)

for your code, try this:

Code: Select all

echo "<a href="http://www.plastikracing.net/images/TSN/".$newfilename."">http://www.plastikracing.net/images/TSN/".$newfilename."</a><br>\n";

Posted: Thu May 26, 2005 12:59 am
by tristanlee85
Thank you all so much. That last code worked. Again, thanks to everyone. No wonder I keep coming back to this site for help!

Posted: Thu May 26, 2005 1:56 am
by hongco
you're very welcome. Helping you also helps me :)

I should've told you about the codes to help you understand better.

$HTTP_POST_FILES['userfile']['name'];
when you echo the above line, it echoes the name of the image you uploaded.

when your file being uploaded, it is saved in a tmp folder on your server - if you use a shared hosting, you won't have direct access to this file via FTP. This file being saved in tmp folder has a different name than the actual name of the image. So if you echo using this :
$HTTP_POST_FILES['userfile']['tmp_name']
you will see the name of the tmp file is different than that of the real fine - by echoing :
$HTTP_POST_FILES['userfile']['name'];

The line:
$newfilename = str_replace(" ", "",$HTTP_POST_FILES['userfile']['name']);

is used to rename your actual image file's name.
photo 1.jpg --> photo1.jpg

what the copy()function does is to copy the file in the tmp folder and save the file with the new name ($newfilename) to your desired folder.

At this point, you would have file: photo1.jpg saved.

Code: Select all

$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path .
$newfilename);
Therefore, to show the real link to the file being uploaded, you have to point it to the $newfilename (photo1.jpg) where the file being saved. If you used $HTTP_POST_FILES['userfile']['name'], the link will show: photo 1.jpg ...and this file was not saved on your desired folder. That's why it didn't work.

........

too wordy? i can't help it lolz