View Full Version : Uploading files with PHP
leebo
02-26-2002, 04:48 PM
First of all i must say what a great new Logo it is well smart :D
I have yet another problem using php, I have got a page to upload a picture but i need to find away to change the name of the file to the username.
i.e. a user find a picture they want lets say c:/windows/desktop/picture1.gif
The username will be sent via another page
www.example.com?username=leebo
I need to save the file as the username i.e. leebo.gif
Any ideas ?
Cheers
Admin
02-27-2002, 05:03 AM
Post the code you are using to save the picture now.
Basically replace $_FILES['imagefile']['filename'] with the username when copying it.
leebo
02-27-2002, 03:55 PM
Thanks here is the main code which uploads it :
function do_upload () {
global $uploadfile, $uploadfile_name, $uploadfile_type, $uploadfile_size;
global $local_file, $error_msg;
//if file specified isn't a gif/jpeg, output error message
if (($uploadfile_type != "image/gif") && ($uploadfile_type != "image/jpeg")
&& ($uploadfile_type != "image/pjpeg"))
{ $error_msg .= "Your file is not a web graphic (GIF/JPG).\n"; }
// Change $upload_dir to the directory that you have write permission (777) for on your server
$upload_dir = "tempphotos";
$local_uploadfile = "$upload_dir/$uploadfile_name";
//if there's no error, print info about the uploaded file
if (!$error_msg && copy($uploadfile, $local_uploadfile)){
echo "Redirecting back to site please wait...... ";
echo" <script>
window.location=\"cars.asp\"
</script> ";
}
Cheers ;)
Admin
02-27-2002, 04:01 PM
First you should use $_FILES when handling uploads, more secure. :)
Other than that, in $local_uploadfile replace $uploadfile_name with $username (don't forget to globalize it in the function), and append either .gif or .jpeg according to $uploadfile_type.
leebo
03-05-2002, 04:42 PM
Thanks firefly
BUT i`m not a PHP person so if you could help me to rename the file that would be great. I got the filename to rename but not the ext. If i uploaded lee.gif i got it to rename it $username with out the .gif - how can i just change the name and leave the extension in place ??
Admin
03-05-2002, 04:52 PM
Use this as the filename:
$username . '.' . substr(strrchr($uploadfile_name, '.'), 1)
So $local_uploadfile would look like this:
$local_uploadfile = $upload_dir . '/' . $username . '.' . substr(strrchr($uploadfile_name, '.'), 1);
leebo
03-05-2002, 05:05 PM
GREATTTTTTTTTTTTTTTTTT !
Thats done it cheers for all your help firefly, you know your onions ! :D
Admin
03-05-2002, 05:08 PM
No probs, leebo. :)
vBulletin® v3.8.12 by vBS, Copyright ©2000-2024, vBulletin Solutions Inc.