0

I am not sure why this code is breaking

mkdir("upload/".$username.'/'.$title.'/', 0700);
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/".$username.'/'.$title.'/' . $_FILES["file"]["name"]);
      echo "Stored in: " ."upload/".$username.'/'.$title.'/' . $_FILES["file"]["name"];
      $link = "upload/".$username.'/'.$title.'/' . $_FILES["file"]["name"];

Here are my errors

Warning: mkdir() [function.mkdir]: No such file or directory in /Volumes/shared/Digital/_Websites/_TEST/qpm/submit.php on line 101

Warning: move_uploaded_file(upload/test/test/Hand Over.docx) [function.move-uploaded-file]: failed to open stream: No such file or directory in /Volumes/shared/Digital/_Websites/_TEST/qpm/submit.php on line 103

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/Applications/MAMP/tmp/php/phpYvo5b5' to 'upload/test/test/Hand Over.docx' in /Volumes/shared/Digital/_Websites/_TEST/qpm/submit.php on line 103 Stored in: upload/test/test/Hand Over.docx1 record added

I;m sure i had this working and now I have somehow broke it?

the echo stored in echos the correct string so not sure why the mkdir is failing, apologies if this a simple fix

3
  • Use absolute paths for file functions. Commented Sep 4, 2013 at 8:07
  • 2
    Don't use $_FILES["file"]["name"] to create files on your system. Commented Sep 4, 2013 at 8:07
  • mkdir() has 3rd parameter called $recursive. Set it to true if you are generating multilevel directories. Commented Sep 4, 2013 at 8:17

2 Answers 2

1

There are a few things you should know about mkdir():

  1. By default, it can only create a directory if the parent directory exists; it can create intermediate directories if you pass true as the third parameter.

  2. Without an absolute path, it's hard to say where your directory will get created. You can either use a configuration file to store this or use a combination of __FILE__, __DIR__ and dirname() to derive it.

I would further advice never to use the value of $_FILES['file']['name'] directly to create files on your server; use tempnam(), optionally in combination with a sanitized version of the original file name.

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

Comments

0

first it is strongly recommended to use absolute path instead of relative path.because relative path may not be working well if you run this php script in an other path.
take this for eg:

test.php:
/home/donald/test.php

<?php
    mkdir("test_dir");

when pwd is "/home/donald/" you run "php test.php" it will make a "test_dir" in /home/donald/

when pwd is "/home/" you run "php donald/test.php" it will a make "test_dir" in /home/


and you can also add true to mkdir() as the 3rd param to mkdir recursively or just use "exec('mkdir -p yourpath')" if it's allowed

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.