24

I want to upload files with PHP and i use move_uplload_files to copy them to the destination folder I want, everything works fine with this :

if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], './uploades/'))
die("success");
else
die("error");

But when I try this

$rand =  chr(rand(97, 122)). chr(rand(97, 122)). chr(rand(97, 122));
if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], './uploades/'.$rand))
die("success");
else
die("error");

I will get error, and it looks like move_uploaded_files can not create folders. How can I do this ?

Basically I am looking for a way to do it like file_put_contents() that creates the path if not exist.

5 Answers 5

42

Use mkdir().

If you need to make multiple folders, such as by passing a/b/c, set the third argument to TRUE.

You can test if it is already there, and add if not like so....

$path = 'abc';

if ( ! is_dir($path)) {
    mkdir($path);
}
Sign up to request clarification or add additional context in comments.

6 Comments

@mkdir() would suppress the error, or the best way would be file_exists
So first i should see if it exists and then creat it if its no there right ?
@Ragez: I think there should be something to do it like file_put_contents() does it automatically creates if it not exists.
@Omeid Herat There is, and it's above :P
@Omeid: I would like so but I didn't ran in that function so far
|
6

Use something like this:

$folder = "uploads"; 
if(!is_dir($folder)) mkdir($folder);

is_dir() checks if the folder is there.

Comments

2

This works for me:

$path = "upload/";
$name = $_FILES["file"]["name"];
// Remove dangerous characters from filename.
$name = str_replace('..', '', $name);
$name = str_replace('/', '', $name);
$name = str_replace('\\', '', $name);

if (($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
&& ($_FILES["file"]["size"] < 2000000)) {
      if ($_FILES["file"]["error"] > 0) {
        echo "Error " . $_FILES["file"]["error"] . "<br>";
      } else {
        if(file_exists($path.$name)){
            echo "$path$name already exists. ";
        } else {                
            @mkdir($path, 0666, true);  // Create non-executable upload folder(s) if needed.
            move_uploaded_file($_FILES["file"]["tmp_name"], $path.$name);
            echo "Stored in: $path$name";
        }
    }
} else {
    echo "Invalid file. Allowed are JPG smaller than 2 MB.";
}

Comments

1

Create the directory first using mkdir()

  $rand =  chr(rand(97, 122)). chr(rand(97, 122)). chr(rand(97, 122));
    mkdir('./uploades/'.$rand);
    if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], './uploades/'.$rand))
    die("success");
    else
    die("error");

Comments

-3
if($_FILES['file_up']['type']=='image/jpeg' || $_FILES['file_up']['type']=='image/png' || $_FILES['file_up']['type']=='image/gif')
{
    move_uploaded_file($_FILES['file_up']['tmp_name'],'uploads/'.time().$_FILES['file_up']['name']);
}
else
{
    echo "Upload only image file..";
}

1 Comment

please edit your answer and format the code to make it readable

Your Answer

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