Jump to content

preg_replace


kyleldi

Recommended Posts

My form uploads 3 attached files to a webserver and emails the admin, but people have the tendency of uploading files with spaces in the name, making the URLs emailed to the admin broken.  I've attempted to use preg_replace to remove the spaces in the files ($file01-$file03), but though the script does not error out, the spaces still remain in the uploaded filenames.  I've tried str_replace as well, to no avail.  Any ideas? Any help would be appreciated!

 

$file01=$HTTP_POST_FILES['file01'];
$file01=preg_replace("( )","_", $file01);
$file02=$HTTP_POST_FILES['file02'];
$file03=$HTTP_POST_FILES['file03'];
if($name=="" || $email=="" || $instructions=="" ){
$errors=1;
$error.="<li>You did not enter one or more of the required fields. Please go back and try again.";
}
if($HTTP_POST_FILES['file01']['tmp_name']==""){ }
else if(!is_uploaded_file($HTTP_POST_FILES['file01']['tmp_name'])){
$error.="<li>The file, ".$HTTP_POST_FILES['file01']['name'].", was not uploaded!";
$errors=1;
}
if($HTTP_POST_FILES['file02']['tmp_name']==""){ }
else if(!is_uploaded_file($HTTP_POST_FILES['file02']['tmp_name'])){
$error.="<li>The file, ".$HTTP_POST_FILES['file02']['name'].", was not uploaded!";
$errors=1;
}
if($HTTP_POST_FILES['file03']['tmp_name']==""){ }
else if(!is_uploaded_file($HTTP_POST_FILES['file03']['tmp_name'])){
$error.="<li>The file, ".$HTTP_POST_FILES['file03']['name'].", was not uploaded!";
$errors=1;
}
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email)){
$error.="<li>Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;


else{

$dirname = str_replace(" ","_",$name);
if(!file_exists($dirname)) mkdir($dirname, 0755);

$image_list[14] = str_replace(" ","_",$image_list[14]);


$image_part = date("")."".$HTTP_POST_FILES['file01']['name'];
$image_list[14] = $image_part;
copy($HTTP_POST_FILES['file01']['tmp_name'], "files/$dirname/".$image_part);
$image_part = date("")."".$HTTP_POST_FILES['file02']['name'];
$image_list[15] = $image_part;
copy($HTTP_POST_FILES['file02']['tmp_name'], "files/$dirname/".$image_part);
$image_part = date("")."".$HTTP_POST_FILES['file03']['name'];
$image_list[16] = $image_part;
copy($HTTP_POST_FILES['file03']['tmp_name'], "files/$dirname/".$image_part);
$where_form_is="http".($HTTP_SERVER_VARS["HTTPS"]=="on"?"s":"")."://".$SERVER_NAME.strrev(strstr(strrev($PHP_SELF),"/"));
$message

Link to comment
https://forums.phpfreaks.com/topic/71653-preg_replace/
Share on other sites

use str_replace.

 

$file01=str_replace(" ","_", $file01);

 

edit:

Where in the script is the variable $file01 used? Shouldnt the script be more like this:

$file01=$HTTP_POST_FILES['file01']['name'];
$file01=str_replace(" ","_", $file01);

 

And then use that in the $image_part:

$image_part = date("").$file01;
move_uploaded_file($HTTP_POST_FILES['file01']['tmp_name'], "files/$dirname/$image_part");

Link to comment
https://forums.phpfreaks.com/topic/71653-preg_replace/#findComment-360708
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.