0
<?php
    require '../config.php';
   // Edit upload location here   
   $result = 0;
   $name = mysql_real_escape_string($_FILES['myfile']['name']);
   $path = csv;
   $ext = 'csv';
   $md5 = md5($name);
   $target_path = $path . '\\' . $md5 . '.' . $ext;

   if(move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
      $result = 1;
   }

   sleep(1);


?>

It won't upload any files such as with the file names that contain brackets, etc.

1
  • To show error messages, use error_reporting(E_ALL); Commented Dec 14, 2012 at 23:48

3 Answers 3

2

Don't do:

$name = mysql_real_escape_string($_FILES['myfile']['name']);

Do:

$name = $_FILES['myfile']['name'];

you are getting the MD5 of $name so no reason to clean it as it will produce a 32-char hex string which will not contain any special characters regardless. If a filename contains special chars and you escape using the above the MD5 will completely change.

Sign up to request clarification or add additional context in comments.

Comments

1

The error is likely here:

$path = csv;

Here PHP is looking for a constant with name csv unless you have defined that, it is going to return null, so your $target_path is not being built correctly.

3 Comments

Hi, csv is defined to a directory with right permissions. Also, how can I get it to show any error messages. It is not even showing any error messages.
Why are you using \` in the $target_path` definition. Check to make sure you have error_reporting and display_errors set properly to where you can see PHP errors.
I also don't know why you are using mysql_real_escape_string here. You are not inserting the filename into mysql so I don't know what you gain from this, though I don't think it is your problem unless you don't have a database connection open in which case it will fail.
0

Just one more thing to try is use the pre defined DIRECTORY_SEPERATOR constant while building your $target path so...

$target_path = $path . DIRECTORY_SEPERATOR . $md5 . '.' . $ext;

3 Comments

still not working. I am able to upload files without any special characters in the name
can you give me an example of one you are trying to upload that is failing?
What it sounds like is hapenning is you need to strip out any special characters from the file name that wouldn't be allowed when saving the file. For that, the long but safe approach is to create a function and inside that function define an array of characters that aren't allowed, then return str_replace($badchars, '', $target_path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.