Jagr
Jagr

Reputation: 502

PHP - mkdir file error

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;
}
?>

Upvotes: 1

Views: 470

Answers (1)

Champ Decay
Champ Decay

Reputation: 228

Try

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

Upvotes: 1

Related Questions