Change upload filename?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
LeonLanford
Forum Newbie
Posts: 7
Joined: Sat Dec 06, 2008 11:31 am

Change upload filename?

Post by LeonLanford »

Hi.. I'm new here :D
I already read the tutorials and useful post thread but there's no solution for my problem.

I want to ask how to change filename that got uploaded.

After I upload "he's boss.jpg", the filename at the site become "s boss.jpg".
How to change that? I can't change that after the uploading start..
After the upload finished, I access the filename using $_FILES['file']['name'], it already become "s boss.jpg".

Can anyone help me?

Thanks..
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Change upload filename?

Post by novice4eva »

what are you using?? It sounds like you are uploading into directory rather than DB and i think you must be using move_uploaded_file function, if not: USE IT :D
umm it will be like

Code: Select all

 
$nameyoulike = 'boss';
$uploads_dir = /* SOME LOCATION IN UR SERVER */;
$ext = /* EXTRACT THE EXTENSION OF THE FILE */;
move_uploaded_file($_FILES["file"]["tmp_name"], "$uploads_dir/".$nameyoulike.".".$ext);
 
LeonLanford
Forum Newbie
Posts: 7
Joined: Sat Dec 06, 2008 11:31 am

Re: Change upload filename?

Post by LeonLanford »

novice4eva wrote:what are you using?? It sounds like you are uploading into directory rather than DB and i think you must be using move_uploaded_file function, if not: USE IT :D
umm it will be like

Code: Select all

 
$nameyoulike = 'boss';
$uploads_dir = /* SOME LOCATION IN UR SERVER */;
$ext = /* EXTRACT THE EXTENSION OF THE FILE */;
move_uploaded_file($_FILES["file"]["tmp_name"], "$uploads_dir/".$nameyoulike.".".$ext);
 
I'm using that method also, I mean, the file uploaded name is "he's boss", not I'm the only who choose the name like that, it has " ' ", so the text before " ' " got cut and become "s boss"

I want to know if someone upload file which has " ' " in the name, I can change the " ' " to " _ ", is there some way to do that?

thanks..
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Change upload filename?

Post by novice4eva »

Code: Select all

$fileName = addslashes($fileName);
LeonLanford
Forum Newbie
Posts: 7
Joined: Sat Dec 06, 2008 11:31 am

Re: Change upload filename?

Post by LeonLanford »

novice4eva wrote:

Code: Select all

$fileName = addslashes($fileName);
[/quote]

where do i put that? addslashes? it automatically change " ' " to " _ " ?
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Change upload filename?

Post by novice4eva »

Could you give the full code coz i just tried to upload the file with the same name and i got the file name exactly the same with single quoted and all, the scary part was when i echoed the $_FILES['file']['name'], it already had the single quotes on. Forget about that addslashes thing, i don't think that could be a solution, but then also who knows maybe it'll work for ya

Code: Select all

$fileName=addslashes($_FILES['file']['name']);
LeonLanford
Forum Newbie
Posts: 7
Joined: Sat Dec 06, 2008 11:31 am

Re: Change upload filename?

Post by LeonLanford »

novice4eva wrote:Could you give the full code coz i just tried to upload the file with the same name and i got the file name exactly the same with single quoted and all, the scary part was when i echoed the $_FILES['file']['name'], it already had the single quotes on. Forget about that addslashes thing, i don't think that could be a solution, but then also who knows maybe it'll work for ya

Code: Select all

$fileName=addslashes($_FILES['file']['name']);
Sorry.. Hope you can help.. I don't really understand this :(

Code: Select all

 
<?php
 
if (!empty($_FILES['file']))
{
//echo "<b>Name:</b> ", $_FILES['file']['name'], "<br />";
    $lokasi = "uplot/" . $_FILES['file']['name'];
    $boleh = 1;
    
    //$lokasi = basename($_FILES["file"]["name"]);
    //$lokasi = "uplot/" . str_replace(array('ö', 'ä', 'ü', 'õ', " ", "'"), 
    //array('o', '2', 'y', '6', "_", '_'), $_FILES["file"]["name"]);
    
       move_uploaded_file($_FILES['file']['tmp_name'], $lokasi);
    
       echo "<h2><u>File information</u></h2>";
       echo "<b>Name:</b> ", $_FILES['file']['name'], "<br />";
       echo "<b>Type (MIME):</b> ", $_FILES['file']['type'], "<br />";
       echo "<b>Size (bytes):</b> ", $_FILES['file']['size'], "<br />";
       echo "<b>Temp Location:</b> ", $_FILES['file']['tmp_name'], "<br />";
       echo "<b>True Location:</b> ", $lokasi, "<br />";
       echo "<b>Error Code:</b> ", $_FILES['file']['error'];
}
else
{
 
    echo "<form enctype='multipart/form-data' action='$_SERVER[PHP_SELF]' method='post'> <br />";
    //echo "<input type='hidden' name='MAX_FILE_SIZE' value='' /> <br />";
    echo "Choose a file to upload: <input type=\"file\" name=\"file\" />";
    echo "<input type='submit' value='Upload' /> <br />";
    echo "</form>";
}
?>
 
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Change upload filename?

Post by novice4eva »

Humm i tried the code you gave me - it uploads the image(with that name he's boss) properly!! I am now wondering if your OS hates single quotes in the file name :roll:
try enabling error, may be it will give us some hint, put these lines at the top of your code and see what errors the script throws

Code: Select all

 
    ini_set('display_errors', 1);
    ini_set('error_reporting', E_ALL);
 
LeonLanford
Forum Newbie
Posts: 7
Joined: Sat Dec 06, 2008 11:31 am

Re: Change upload filename?

Post by LeonLanford »

I'm using xp sp 2.. :(

I already added your code but there's no other information beside my usual info..
File information
Name: s boss.jpg
Type (MIME): image/jpeg
Size (bytes): 4756
Temp Location: C:\Program Files\xampp\tmp\php7AD.tmp
True Location: uplot/s boss.jpg
Error Code: 0
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Change upload filename?

Post by novice4eva »

Did you try addslashes??

Code: Select all

 
 
<?php
 
if (!empty($_FILES['file']))
{
    $fileName = addslashes($_FILES['file']['name']);
    $lokasi = "uplot/" .$fileName ;
    $boleh = 1;
   
  
       move_uploaded_file($_FILES['file']['tmp_name'], $lokasi);
   
       echo "<h2><u>File information</u></h2>";
       echo "<b>Name:</b> ", $fileName, "<br />";
       echo "<b>Type (MIME):</b> ", $_FILES['file']['type'], "<br />";
       echo "<b>Size (bytes):</b> ", $_FILES['file']['size'], "<br />";
       echo "<b>Temp Location:</b> ", $_FILES['file']['tmp_name'], "<br />";
       echo "<b>True Location:</b> ", $lokasi, "<br />";
       echo "<b>Error Code:</b> ", $_FILES['file']['error'];
}
else
{
 
    echo "<form enctype='multipart/form-data' action='$_SERVER[PHP_SELF]' method='post'> <br />";
    //echo "<input type='hidden' name='MAX_FILE_SIZE' value='' /> <br />";
    echo "Choose a file to upload: <input type=\"file\" name=\"file\" />";
    echo "<input type='submit' value='Upload' /> <br />";
    echo "</form>";
}
?>
 
 
hope it solves it, i haven't tried this code thou!! :banghead: PLZZ WORK :D
LeonLanford
Forum Newbie
Posts: 7
Joined: Sat Dec 06, 2008 11:31 am

Re: Change upload filename?

Post by LeonLanford »

still not working.. thanks a lot anyway :D

I just tried the code in my hosting server.. and it's worked.. it seems my xampp or apache error..

Thanks again.. :D
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Change upload filename?

Post by novice4eva »

It has to do with magic quote gpc setting. Do this, check the get_magic_quotes_gpc() and do something like this

Code: Select all

 
if (!get_magic_quotes_gpc()) {
    $fileName = addslashes($_FILES['file']['name']);
} else {
    $fileName = $_FILES['file']['name'];
}
 
 
If it doesn't work try the reverse, it is early morning and i am half sleep.....i can;t get my mind on this slashes n no slashes thing rite now :drunk:
LeonLanford
Forum Newbie
Posts: 7
Joined: Sat Dec 06, 2008 11:31 am

Re: Change upload filename?

Post by LeonLanford »

still not working.. :D
File information
Name: s boss.jpg
Type (MIME): image/jpg
Size (bytes): 4756
Temp Location: C:\Program Files\xampp\tmp\php136.tmp
True Location: uplot/s avidemux.jpg
Error Code: 0
anyway it's working in my hosting so it's not really problem now.. thanks :D
Post Reply