1

When i submit a file into my file directory, i get a lot of errors like

( ! ) Warning: mkdir(): No such file or directory in C:\wamp64\www\MT\directory.php on line 38

( ! ) Warning: move_uploaded_file(devPacks/peopl39e_8503_cheese/0kk/1501471127_adc9ebd871a68b573fd928ebff5bb54b.png): failed to open stream: No such file or directory in C:\wamp64\www\MT\directory.php on line 40

from the directory. I believe the problem is in this line of code

$dir = "devPacks/" .$fullname."_".$userid."_".$username."/".$packname;

because when i exclude "$packname" like this

$dir = "devPacks/" .$fullname."_".$userid."_".$username."/".$packnames;

Notice i added an "s" to the send of packnames* and i get this

( ! ) Notice: Undefined variable: packnames in C:\wamp64\www\MT\directory.php on line 35

and when i echo the directory out i get this "devPacks/peopl39e_8503_cheese/ ". So i am not sure what is going on but here is my code.

PHP

<?php
if($_SERVER['REQUEST_METHOD'] =="POST"){
    $currentDirectory = getcwd();
    $userid = "8503";
    $fullname = "peopl39e";
    $username = "cheese";
    $packname = "0kk";
    foreach($_FILES['file']['tmp_name'] as $key => $error){
        if ($error != UPLOAD_ERR_OK) {
            $errors[] = $_FILES['file']['name'][$key] . ' was not uploaded.';
            continue;
        }
        $file_tmp = file_get_contents($_FILES['file']['tmp_name'][$key]);
        //keep only A-Z and 0-9 and everything else KILL
        $file_name = preg_replace("/[^a-z0-9\.]/", "_", strtolower($_FILES['file']['name'][$key]));
        $file_name = strtotime("now")."_".$file_name;
        $allowed =  array('gif','png' ,'jpg');
        $ext = pathinfo($file_name, PATHINFO_EXTENSION);
        if(!in_array($ext,$allowed) ) {
            die('error');
        }
        $dir = "devPacks/" .$fullname."_".$userid."_".$username."/".$packname;
        if(is_dir($dir)==false){
            mkdir($dir, 0777);
        }
        if(!move_uploaded_file($_FILES['file']['tmp_name'][$key],$dir.'/'.$file_name)){
            die("File didn't send!");
        }
    }
    echo $dir;
}
?>
5
  • 1
    You need to use the 3rd recursive parameter to create nested directories, ie mkdir($dir, 0777, true). See php.net/manual/function.mkdir.php Commented Jul 31, 2017 at 4:06
  • try DIR php.net/manual/ru/language.constants.predefined.php Commented Jul 31, 2017 at 4:59
  • Thanks @Phil that worked. Commented Jul 31, 2017 at 5:02
  • @Jagr no problem. The error is telling you that it can't create the last part of the directory path because the previous part does not exist, ergo "No such file or directory" Commented Jul 31, 2017 at 5:04
  • ohhh i see, can you post as a question so you can be given credit. Commented Jul 31, 2017 at 5:06

1 Answer 1

1

Try

For Upload File path $_SERVER['DOCUMENT_ROOT'];

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

Comments

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.