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 . '[/IMG]</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..