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
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
Share on other sites

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.