• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

Uploaded files

Hi, I need some help with an upload problem. Some of the files that members are uploading have spaces in the name, ex. file name.pdf. When the url is created it can't find this file because of the blank space. How do I trim this?

Here is how I access the download file:

 <?PHP if ($row_events['download1'] == NULL) {echo "";} else { echo "<img src=\"../images/pdficon.gif\" alt=\"pdf\" width=\"20\" height=\"22\" align=\"absmiddle\" /> <a href=\"../admin/upload/$row_events[download1]\" target=\"_blank\">$row_events[download1]</a>";} ?>

Thank you for your help. Have a good day.

2 Solutions
use urlencode() .. code attached =)
if ($row_events['download1'] == NULL) {echo "";} else { echo "<img src=\"../images/pdficon.gif\" alt=\"pdf\" width=\"20\" height=\"22\" align=\"absmiddle\" /> <a href=\"../admin/upload/".urlencode($row_events[download1])."\" target=\"_blank\">$row_events[download1]</a>";} 

Open in new window

pertrai1Author Commented:
Hey emphamy, thank you for the reply. It looks like my problem is that the file is uploading to the database but not to the directory supplied.

Could you take a look at my code snippet and tell me what I need to change to get the file uploaded to the directory as well. Thank you much.

		$fileName = $_FILES['userfile']['name'];
		$tmpName  = $_FILES['userfile']['tmp_name'];
		$fileSize = $_FILES['userfile']['size'];
		$fileType = $_FILES['userfile']['type'];
      'upload/' . $_FILES['userfile']['name']);
      echo "Stored in: ' . 'upload/' . $_FILES['userfile']['name']";
		$fp = fopen($tmpName, 'r');
		$content = fread($fp, $fileSize);
		$content = addslashes($content);
			$fileName = addslashes($fileName);
		$query = "INSERT INTO upload (name, size, type, content ) ".
		         "VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
		mysql_query($query) or die('Error, query failed');				
		echo "<br>File $fileName uploaded<br>";
<form action="" method="post" enctype="multipart/form-data" name="uploadform">
  <table width="500" border="0" cellpadding="1" cellspacing="1" class="box">
      <td width="400"><input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="userfile" type="file" class="box" id="userfile" size="50">
      <td width="100"><input name="upload" type="submit" class="box" id="upload" value="  Upload  "></td>

Open in new window

your upload script is ok, maybe it is a folder permission problem
to overcome that blank space problem, u can replace and strange chr to _
a simple script like this can get it done

$arr= array(' ','!','*','%','\\','/');
$hay = "this is a good ! @ example # $%%";
$hay= str_replace($arr,'_',$hay);
echo $hay;

also, change line 12 to
echo "Stored in: " . 'upload/' . $_FILES['userfile']['name'];

In your code. The line:
move_uploaded_file($_FILES['userfile']['tmp_name'], 'upload/' . $_FILES['userfile']['name']);

move the uploaded files from temporary directory to the correct directory. Make sure that you have a directory named 'upload' with write permission for the account execute this PHP script, and that directory has to reside in the same place as the PHP script.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now