tags:

views:

389

answers:

3

1.Hi, I have a internal use only file upload script that uploads the files to a directory. When I upload something from my computer with a spcace in the name i.e example 1.zip it uploads with a space in the name thus killing the link in a email. Is it possible to make apache remove the space when its uploaded or make it a underscore?

The second problem I am having is how would I parse this to make the link an email link with the url of the file as the body of the email amd the email addy anything? Thanks in advanced

if (move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir . $_FILES['file']['name'])) {
    // uploaded file was moved and renamed succesfuly. Display a message.
    echo "Link: " . "http://example.org/" . $_FILES["file"]["name"];
+2  A: 

You just need to urlencode() your file name and everything is fine:

echo "Link: http://example.org/" . urlencode($_FILES["file"]["name"]);

But if you want to remove the spaces for another reason, you can use str_replace():

$replaced_name = str_replace(' ', '_', $_FILES["file"]["name"]);
rename($uploaddir . '/' . $_FILES['file']['name'], $uploaddir . '/' . $replaced_name);
# You should urlencode() it nonetheless:
echo "Link: http://example.org/" . urlencode($replaced_name);
soulmerge
How would I make it so I could use that link as a mailto like the link would go on the body and anything would be in the message
newhen
Have a look at the examples section of the according RFC: http://tools.ietf.org/html/rfc2368#section-6
soulmerge
+1  A: 

Try:

$filename = $_FILES['file']['name'];
$filename = preg_replace("/[^a-zA-Z0-9]/", "", $filename);

//then
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir . $filename)) {

// uploaded file was moved and renamed succesfuly. Display a message.
echo "Link: " . "http://example.org/" . $filename;
Nathan
Thanks! any ide on how I would do an email like thing
newhen
You want to send this link for an email?
Nathan
Basically I want it to echo a mailto:[email protected] with the body text at the link .
newhen
You can use mailto:[email protected]?body=http://example.com/file.zip. For a complete reference on mailto check: http://www.ianr.unl.edu/internet/mailto.html :)
Nathan
+1  A: 

As a side note : with the code you are using, what is happening if two files with the same name are uploaded ? If you don't do a check (like "is there a file that already has that name in $uploaddir ?") the second file will replace the first one.

That might not be something you want... is it ?

If not, to solve that (potential) problem, one solution is to always rename uploaded files, with names you control. (A simple counter would probably to the trick)

Another thing is : $_FILES["file"]["name"] is sent by the client, and, as such, can probably be forged to contains whatever someone would want. If it contains something like "../../index.php" (or something like this - you get the idea), this could allow someone to put any file they want on your server.

To prevent this from happening, you shoud be sure the file name/path used as destination of move_uploaded_file does not contain anything "dangerous". A solution could be to use basename. (see, for instance, example #2 on POST method uploads)

You might also want to check the mimetype of the uploaded file, so you don't get executables, for instance -- and you should make sure files uploaded are not executable by the webserver.

Pascal MARTIN