Page 1 of 1
Change upload filename?
Posted: Sat Dec 06, 2008 11:41 am
by LeonLanford
Hi.. I'm new here

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..
Re: Change upload filename?
Posted: Sun Dec 07, 2008 3:02 am
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
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);
Re: Change upload filename?
Posted: Sun Dec 07, 2008 5:31 am
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
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..
Re: Change upload filename?
Posted: Sun Dec 07, 2008 7:23 am
by novice4eva
Code: Select all
$fileName = addslashes($fileName);
Re: Change upload filename?
Posted: Sun Dec 07, 2008 7:50 am
by LeonLanford
novice4eva wrote:Code: Select all
$fileName = addslashes($fileName);
[/quote]
where do i put that? addslashes? it automatically change " ' " to " _ " ?
Re: Change upload filename?
Posted: Sun Dec 07, 2008 10:00 pm
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']);
Re: Change upload filename?
Posted: Sun Dec 07, 2008 10:27 pm
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>";
}
?>
Re: Change upload filename?
Posted: Mon Dec 08, 2008 6:11 am
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
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);
Re: Change upload filename?
Posted: Mon Dec 08, 2008 7:00 am
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
Re: Change upload filename?
Posted: Mon Dec 08, 2008 8:41 am
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!!

PLZZ WORK

Re: Change upload filename?
Posted: Mon Dec 08, 2008 8:50 am
by LeonLanford
still not working.. thanks a lot anyway
I just tried the code in my hosting server.. and it's worked.. it seems my xampp or apache error..
Thanks again..

Re: Change upload filename?
Posted: Mon Dec 08, 2008 8:25 pm
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

Re: Change upload filename?
Posted: Tue Dec 09, 2008 8:27 am
by LeonLanford
still not working..
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
