Jump to content

[SOLVED] PHP Help - Replacing Space with underscore in filenames


Gamerz

Recommended Posts

Hello,

I currently have a file uploader website.

 

I have seen many filename's uploaded to my server with a space...making the file url's in the e-mail broken because of the space.

 

Is there any possible way so that before uploading the file, the system automatically replaces the space with an underscore...or if the file name is already one word with no space, it just ignores it?

 

If it's possible, where should I put the code? Below are my scripts..

 

Here is the upload form:

<form action="./upload.php" method="post" enctype="multipart/form-data">

      <label for="file">Select a file:</label> 
      <input type="file" name="userfile" id="file" size="20"><br><label for="file">   <p align="center">
<img src="loader.gif" name="loading" style="visibility:hidden;" width="220" height="19"><p align="center">
<input type="Submit" style="visibility:visible;" name="upload"  value="Upload File" onclick="this.style.visibility='hidden'; loading.style.visibility='visible'">  <p align="center">


</form>

 

 

 

 

Here is the actual process upload form to upload the actual file:

 

<?php
        $max_filesize = 10485760; // Maximum filesize in BYTES (currently 0.5MB).
       $download='download.php?file=';
      $upload_path = 'uploads/'; // The place the files will be uploaded to (currently a 'files' directory).
       $url = 'uploads/'; // The place the files will be uploaded to (currently a 'files' directory).
$url2 = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://';
$url2 .= $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
   $filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
   $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
$email = $_POST['email'];
$sendto = "$email";
$subject = "$name uploaded a file to your domain";
$message = "Name: $name\n\nEmail: $email\n\nCompany: $company\n\nFile name: $filename";
   // Now check the filesize, if it is too large then DIE and inform the user.
   if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
      die('<center><h1>The file you attempted to upload is too large.<br>We have a 10MB/Per file Limit</center></h1>');

   // Check if we can upload to the specified path, if not DIE and inform the user.
   if(!is_writable($upload_path))
      die('You cannot upload to the specified directory, please CHMOD it to 777.');

    // This restrics certain types of files from being uploaded.
     if ($_FILES['imagefile']['size'] > 100000000  )
{
die ("<center><h1>The file you attempted to upload is too large.</center></h1>");

}

$blacklist = array(".php",".htm",".aspx",".dll",".exe",".html",".css",".phtml", ".php3", ".php4", ".js", ".shtml", ".pl" ,".py");
foreach ($blacklist as $file)
{
if(preg_match("/$file\$/i", $_FILES['userfile']['name']))
{
echo "<center><h1>Error: The file you attempted to upload is restricted!</center></h1>\n";
exit;
}
}
   // We'll start handling the upload in the next step
  // Upload the file to your specified path.
   if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
  
         echo '<h3 align="center">Success! Your file has been uploaded.<strong><br>
mail($sendto, $subject, $message);

Your file/image upload of <em>'. $filename . '</em> 
was successful.</strong></h3>
	 </p>
<form action="upload22.php" method="post" enctype="multipart/form-data">
<p align="center"><strong>Your uploaded file/image is now live on our servers. 
        Scroll down below to obtain the URL to your file/image. Optionally, you can use one of the embed methods which have been generated for you.</strong></font></p>
	<p align="center">You can also send the uploaded file URL (' . $url2 . $url . $filename. ') to your e-mail by using the form below: </p>

<p align="center"><b>E-mail to send file URL:</b> <br> <input type="email" name="email" id="email" size="40"><p align="center"><label for="email">  <input type="submit" name="email" />
</form>
    <center> <table border="0" cellspacing="10" cellpadding=0">
	 <tr>
	 <td><center>If you just need the general link to your uploaded file/image, then 
         use this code below:</td>
	 </tr>
	 <tr>
         <td><strong><center>Direct viewing link to your 
         uploaded file/image:</font></strong><center>  <textarea cols="35" rows="3" onfocus="this.select()" onclick="this.select()">' . $url2 . $url . $filename . '</textarea><br><br>
         <strong>Download link of your uploaded file/image:
         </font></strong><br><textarea cols="35" rows="3" onfocus="this.select()" onclick="this.select()">' . $url2 . $url . $download . $filename . '</textarea></td>
         </tr>
	 </table>
	<center>  <table border="0" cellspacing="10" cellpadding="0" height="114">
	 <tr>
	 <td height="19">If you uploaded an image and need it embedding in a 
         forum, then use this code below</font>:</td>
	 </tr>
	 <tr>
	 <td height="75"><strong><center> Embedding the image on MySpace, Facebook, YouTube, 
         or even a forum:</font></strong><center> <textarea cols="35" rows="3" onfocus="this.select()" onclick="this.select()">[img=' . $url2 . $url . $filename . ']</textarea></td>
	 </tr>
	 </table>
	 <table border="0" cellspacing="10" cellpadding="0">
	 <tr>
	 <td><center> If you want a hyperlink for your file/image, then refer to 
         the below:</font></td>
	 </tr>
	 <tr>
	 <td>
	 <strong><center> Hotlink for Websites</font></strong>:</font><br/> <textarea cols="35" rows="3" onfocus="this.select()" onclick="this.select()"><a href="' . $url2 . $url . $filename . '"></a></textarea></td>
         </tr>
         </table>
         <table border="0" cellspacing="10" cellpadding="0" height="114">
	 <tr>
	 <td height="19">Share your uploaded file/image by referring below:</font></td>
	 </tr>
	 <tr>
	 <td height="75"><strong><center> Sharing your 
         uploaded file/image to 
         your friends</font>:</strong><br/> <center> <textarea cols="35" rows="3" onfocus="this.select()" onclick="this.select()">' . $url2 . $url . $filename . '</textarea></td>
	 </tr>
	 </table>
<br><br><center><a href="."><font size="5" color="#000080"><b>Click here to upload another file!<br>
         </b></font><font size="4"><br></font></a>

	 ';
       else
         echo '<center><h1>There was an error during the file upload.  Please try again.</font></center><br><br></h1>'; // It failed .
?>

 

 

~Thank you so much guys...you helped helped me a lot, but i really need this help..

Link to comment
Share on other sites

mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

 

when i put that above    if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))

 

 

it doesnt work...

Link to comment
Share on other sites

Do you really have to ask a question like that when someone dumps an entire script on the forum instead of showing relevant code snippets?

Point taken, would be nice if people who use PHP actual learn PHP,

 

mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

 

when i put that above    if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))

 

 

it doesnt work...

it won't work.. because thats the syntax on how to use str_replace!

 

you would use it like this..

if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . str_replace(" ","_",$filename)  ))

Link to comment
Share on other sites

if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . str_replace(" ","_",$filename)  ))

 

It works, but when I upload a file with spacing, it uploads like how I wanted: ex_ample.txt...but the link displays still as with spaces....how do I get rid of the spaces on the link's on my site?

 

For example, when I upload ex ample.txt, the file displays on my server as ex_ample.txt...but on my website, where it tells them the URL....it still display's with spacing...

Link to comment
Share on other sites

What do you mean?

 

No, it's not the link that's covering so I don't actually see the underscore.

 

EDIT: I have a textbox...and inside the textbox has the links in them...so there's not actually a blue underline link...it's just a plain text...lol So I know it doesnt show

Link to comment
Share on other sites

Wait,so how do I put str_replace on the $filename so every filename will automatiically be replacedD

 

Here is the defined filename:   

$filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).

--------

 

How do I add str_replace on that define?

 

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.