Jump to content

Recommended Posts

Hello & happy new year,

 

I have an uploader where people upload files. After the file upload my php sends an email to an with a link to the uploaded image. Everything works great ecept some people put spaces in their file name,

 

example:

my son.jpg

 

so the link it sends via email is:

http://www.mydomain.com/my son.jpg

 

well the space in the name creates problems for me when i then, send a link to the uploaded file.

 

I guess there are 2 options.

 

1. when i write the file to the server remove spaces in the name

2. Replace the spaces in the name with "%20" when i email the link to the uploaded file.

 

My question is which of the above would be better? And how would i implement either of the above. or both of the above :)  I am trying to learn.

 

my upload php

<?php
//create the directory 
if(!is_dir("./files")) mkdir("./files/", 0755); 
//move the uploaded file
move_uploaded_file($_FILES['Filedata']['tmp_name'], "./files/".$_FILES['Filedata']['name']);
chmod("./files/".$_FILES['Filedata']['name'], 0777);
?>

 

my email php

<?php

$sendTo =  $_POST["email"];
$sendTo2 = "mattpaxton@clearchannel.com";
$sendTo3 = "barry@wzzo.com";
$subject = "Thanks for the best rack photo!";

$headers = 'From: contests@wzzo.com' . "\r\n" .
    'Reply-To: WZZO Contests' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

$headers .= "Return-path: " . $_POST["email"];

$message = 'Thank you for registering for our Best Rack competition,

voting begins December 17th. Below is a link to the image you submitted:

http://www.wzzo.com/pages/bestrack08/files/'. $_POST["selectedfile"] . "\r\n \r\n The info that was submitted: \r\n \r\nname - " . $_POST["name"] . "\r\nphone - " . $_POST["phone"] . "\r\nadditional info - " . $_POST["message"] . "\r\nphoto name - " . $_POST["selectedfile"] . "\r\naddress -\r\n " . $_POST["line1"] . "\r\n" . $_POST["line2"] . "\r\n" . $_POST["city"] . ' ' . $_POST["whatstate"] . ' ' . $_POST["zip"] . 

"\r\n\r\nKeep checking the best Rack page out for updates - http://www.wzzo.com/pages/bestrack08/bestrack08.html

PLEASE DO NOT REPLY TO THIS EMAIL. 
For best rack questions / comments email: barry@wzzo.com" ;

mail($sendTo, $subject, $message, $headers);
mail($sendTo2, $subject, $message, $headers);
mail($sendTo3, $subject, $message, $headers);



?>

Also i know my code is not optimized try not to laugh, I am learning with no teacher.

 

Link to comment
https://forums.phpfreaks.com/topic/140021-solved-removing-spaces-in-a-link/
Share on other sites

You really should save the file without a space in the name...

 

<?php
//create the directory 
if(!is_dir("./files")) mkdir("./files/", 0755); 
//move the uploaded file
$name = str_replace(" ", "_", $_FILES['Filedata']['name']);
move_uploaded_file($_FILES['Filedata']['tmp_name'], "./files/".$name);
chmod("./files/".$name, 0777);
?>

 

That way it is replace with an _ and you do not have to worry. But I would do more checking than that, such as only allowing alpha-numeric characters using preg_match cause any other character could potentially cause problems, such as ' would not allow a file to be deleted, etc.

You really should save the file without a space in the name...

 

<?php
//create the directory 
if(!is_dir("./files")) mkdir("./files/", 0755); 
//move the uploaded file
$name = str_replace(" ", "_", $_FILES['Filedata']['name']);
move_uploaded_file($_FILES['Filedata']['tmp_name'], "./files/".$name);
chmod("./files/".$name, 0777);
?>

 

That way it is replace with an _ and you do not have to worry. But I would do more checking than that, such as only allowing alpha-numeric characters using preg_match cause any other character could potentially cause problems, such as ' would not allow a file to be deleted, etc.

 

That is exactly what i needed. Tnx. Another question (same topic)

 

1. Could i add multiple conditions to that var for example:

$name = str_replace(" ", "_", || "#", "_", $_FILES['Filedata']['name']);

 

2. would preg_match take care of multiple file extentions like:

 

myfilename.psd.zip

 

I know in action script there is something called indexOf where i could find the first instance a period exists and replace it, but leave the last instance, but i do not know php well enough.

 

3. Why is renaming the file with _ instead of spaces better then adding the "%20"

 

Thanks gutys for all your help.

 

preg_match would basically throw out anything that is not kosher, since periods, _, - and alpha-numeric are all good for filenames, you would not really need to verify multiple extensions. That would be an unnecessary step imo.

 

$name = str_replace(" ", "_", || "#", "_", $_FILES['Filedata']['name']);

 

Should be

$replaceArr = array(" ", "#");
$name = str_replace($replaceArr, "_", $_FILES['Filedata']['name']);

 

And using _ is better than %20, for SEO reasons and because the file does not get brokenup when trying to link it in emails etc. That and %20 just looks soo ugly, who wants that?

Thanks man. that array structure just taught me a lot, and i did not know the multiple instances of ". " were kosher either.

 

Usually when you have multiple . it should generally be to tell you what is in the zip file. That way you can know, oh it is a PDF that is in there etc. That is not always the case, but generally if you have .ext.ext the last one is the correct one, the one before is what is inside or what it might have used to be.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.