image upload keeps file name ?

image upload keeps file name ?

am 01.04.2010 11:51:15 von Matthew Croud

Hi Guys,

Can someone confirm for me that the code below will move an uploaded
file and give it the same name as the original image file name ?


$file_dir = "/home/uploads";
foreach($_FILES as $file_name => $file_array) {
echo "path: ".$file_array["tmp_name"]."
\n";
echo "name: ".$file_array["name"]."
\n";
echo "type: ".$file_array["type"]."
\n";
echo "size: ".$file_array["size"]."
\n";

$UploadName[$num] = $file_array["name"];
$num++;

if (is_uploaded_file($file_array["tmp_name"])) {
move_uploaded_file($file_array["tmp_name"], "$file_dir/".
$file_array["name"]) or die ("Couldn't copy");
echo "file was moved!
";
}
}



Many thanks,





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: image upload keeps file name ?

am 01.04.2010 11:55:44 von Ashley Sheridan

--=-qPGQUcAWEa4cBrUIin2k
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Thu, 2010-04-01 at 10:51 +0100, Matthew Croud wrote:

> Hi Guys,
>
> Can someone confirm for me that the code below will move an uploaded
> file and give it the same name as the original image file name ?
>
>
> $file_dir = "/home/uploads";
> foreach($_FILES as $file_name => $file_array) {
> echo "path: ".$file_array["tmp_name"]."
\n";
> echo "name: ".$file_array["name"]."
\n";
> echo "type: ".$file_array["type"]."
\n";
> echo "size: ".$file_array["size"]."
\n";
>
> $UploadName[$num] = $file_array["name"];
> $num++;
>
> if (is_uploaded_file($file_array["tmp_name"])) {
> move_uploaded_file($file_array["tmp_name"], "$file_dir/".
> $file_array["name"]) or die ("Couldn't copy");
> echo "file was moved!
";
> }
> }
>
>
>
> Many thanks,
>
>
>
>
>


Yes, the original filename comes from the ["name"] array element.
However, if someone is uploading a filename with the same name as one
that already exists, you will be overwriting it.

For peace of mind, I've always found it best to save the file using the
tmp_name given to it by PHP, and store this against the original
filename in a database. You can then use PHP to deliver the file back to
the user when it's needed as either a download or something displayed in
the browser. This works nicely with storing files outside of the web
root, which will prevent people from maliciously uploading files to
attempt to break your server and/or app.

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-qPGQUcAWEa4cBrUIin2k--

Re: image upload keeps file name ?

am 01.04.2010 12:10:56 von Midhun Girish

--0016367b70fc4e0f5304832a15e7
Content-Type: text/plain; charset=ISO-8859-1

I use the follwing function for moving files:


public function moveFile($file,$targetdir="../uploads/images/")
{
$fileName = $file['name'];
$ext = substr($fileName, strrpos($fileName, '.') + 1);
do
{

$targetfilename=md5(date("m.d.y.h.i.s").basename($fileName)) .'.'.$ext;
$fullname=$targetdir.$targetfilename;
}while(file_exists($fullname));
move_uploaded_file($file["tmp_name"],$fullname);
return $fullname;
}


Call the fn as :

foreach($_FILES as $file_name => $filearray)
{
if( $filearray['error']=='')
{
$filenametobestored=moveFile($filearray);
/*Enter name into db here*/
}
}


this will make sure you never over write anyfiles..


Midhun Girish

On Thu, Apr 1, 2010 at 3:25 PM, Ashley Sheridan wrote:

> of the web
> root, which will prevent people from maliciously uploadi
>

--0016367b70fc4e0f5304832a15e7--