1
votes

I am facing this strange problem with PHP upload script. Hoping to get some help from someone please.

Below is the details of this problem:

DOCUMENT_ROOT is "/var/www/html"

PHP upload works fine to a folder called "/var/www/html/uploads"

Have Another directory hosting different webpage at "/var/www/html/folder" Same PHP upload.sh script can not upload files to "/var/www/html/directory/folder" directory.

Folder permission has been set to 0777 to both "/var/www/html/directory/folder" and "/var/www/html/uploads" Same upload script is used in both folders.

Below value is set in upload.php located in both folders.

$uploads = $_SERVER['DOCUMENT_ROOT'] . "folder/uploads/files/"

I have tried setting absolute path in $uploads as follows but did not work:

$upload_dir = "/var/www/html/files/"

Upload is failing with below error:

Message: fopen(var/www/html/folder/uploads/files/filename.pdf): failed to open stream: No such file or directory
FPDF error: Unable to create output file:(/var/www/html/folder/uploads/files/filename.pdf)

Any idea why the server is not letting upload.php from "/var/www/html/folder" to write to "/var/www/html/folder/uploads/" directory.

No related message could be found in any log files.

=============================================================

It appreas that PHP is not able to mkdir in /var/www/html/folder/uploads/files/.

Piece of code that creates the directory is:

  $uploadPath = "folder/uploads/files/";
  $dir = $_SERVER['DOCUMENT_ROOT'] . $uploadPath;

    if (!is_dir($dir)) @mkdir($dir, 0777);
    return $uploadPath;

Any idea why it is unsable to to do so with the above mkdir code?

Thank you.

2
It probably is but you are trying to write/open a different folder i.e./var/www/html/folder/uploads/ --- var/www/html/folder/uploads/files/ - user2417483
Hi Jeff, Thank you for the reply. It is a subdirectory and have given 777 permission for it too. I just can't understand why it can not create file within /var/www/html/folder/uploads/. - BBT

2 Answers

0
votes

make sure that file /var/www/html/folder/uploads/files/filename.pdf is not open anywhere when you run script. if it's open somewhere else then TCPDF can't open it.

0
votes

For those who are facing similar problems, my issue was resolved after adding recursive flag. My mkdir command was changed as follows:

From:

if (!is_dir($dir)) @mkdir($dir, 0777);

To:

if (!is_dir($dir)) @mkdir($dir, 0777, true);