PHP Make Directory - php

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

There are a few things you should know about mkdir():
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.
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.

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

Related

Warning: include_once(../settings/settings.php): failed to open stream - but the path is actually right?

Warning: include_once(../settings/settings.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/Project/classes/user.class.php on line 2
Warning: include_once(): Failed opening '../settings/settings.php' for inclusion (include_path='.:/Applications/MAMP/bin/php/php7.2.1/lib/php') in /Applications/MAMP/htdocs/Snapshot/classes/user.class.php on line 2
But the file location is actually right? It even gives me auto path fill suggestions (visual code plugin) to that path!
Why does this happen is this something to do with the / or the second warning line?
So I'm trying to include the settings.php file from inside the User.class.php file with the following code.
include_once("../settings/settings.php");
../ -> goes back into the root folder
settings/ -> enters settings folder
settings.php -> should locate the needed .php file
Replace
include_once("../settings/settings.php");
with
include_once(__DIR__ . "/../settings/settings.php");
__DIR__ will resolve in the absolute path of the file where this constant is used. You can navigate your folders from that.
Use an absolute path to the file. You can use $_SERVER['DOCUMENT_ROOT'] or create a const variable BASEPATH that you can easily set root directory value to.
include_once($_SERVER['DOCUMENT_ROOT']."/settings/settings.php");
OR
define('BASEPATH', "/Applications/MAMP/htdocs");
include_once(BASEPATH."/settings/settings.php");
Depending on where you call your class. Better yet is to use configuration file to fulfil the require/include path like every PHP framework does.

mkdir giving false warning when creating a directory

I am trying to create a temporary directory but I am getting following error
Warning: mkdir(): File exists
However when i checked the directory actually doesn't exist.A typical value for $tmp is /tmp/testKanfEt
$tmp = tempnam(sys_get_temp_dir() , 'test');
echo $tmp;
mkdir($tmp);
What am i missing here ?
The function tempnam() creates a file in given path and returns the full path if created successfully. You are attempting to make a directory with the same path as the file.
But in this case, tempnam() is being used wrongly in my opinion.
It should be used when trying to make tmp files in any other directory than the o.s. tmp folder.
Why? Because making files in the tmp directory you should not care about the file name because once the lock has been released on a file (with fclose() or end of script execution for example) you cannot guarantee the file still being there.
Instead, use tmpfile() as it returns a file handle directly creating a file in the tmp directory.
And if you really need the file name, you still could use tempnam() or retreive it from the handle like so:
echo stream_get_meta_data(($fh=tempfile()))["uri"];

Why does this call to fopen fail when all the other elements appear correct?

Yes, another question about fopen()... There are hundreds but none of them got this. This question is very similar, but after trying the answer (check permissions, check the tree exists), I still have this problem.
When trying to use fopen to create a file, I call the function and give it the required parameters, yet it throws a warning: "No such file or directory". However, checking the tree reveals the directory does indeed exist. The code goes like this:
$target_dir = "/files/" . usernameIs() . "/";
$target_file = $target_dir . basename($_FILES["uploadedFile"]["name"]);
...
$file = fopen($target_file, "w+");
fclose($file);
$target_file ends up containing /files/ArtOfCode/icon.png (the username is ArtOfCode and the name of the uploadedFile that I'm using to test is icon.png.
The directory tree looks like this:
public_html
> /upload
> /upload.php (the calling script)
> /action.php (this script)
> /files
> /ArtOfCode
I get two warnings, both relating to fopen():
Warning: fopen(/files/ArtOfCode/icon.png): failed to open stream: No such file or directory in /home/u875642593/public_html/upload/action.php on line 26
Warning: fclose() expects parameter 1 to be resource, boolean given in /home/u875642593/public_html/upload/action.php on line 27
So, I'm aware that the file (/files/ArtOfCode/icon.png) doesn't exist. However, the PHP docs say "...if the file does not exist, fopen will create it before trying to use it." The supporting directory tree does exist, so I'm stumped. How do I make this call create the file?
PHP's filesystem functions like fopen() and its siblings operate on filesystem paths, not on paths relative to your web server's document root ("web" paths). (They may also operate on streams like http://example.com/file.txt if your PHP installation is so configured, but that's not relevant here).
Since your path to fopen() begins with a /, the filesystem looks for it in the filesystem's root. A / at the beginning of a filesystem path always represents an absolute path rather than a relative one, and the directory structure doesn't actually exist in that absolute location.
You need to find the files/ directory which resides in your web server's document root. Accessing it from a PHP script inside the uploads/ directory, you can do that with the .. pseudo-directory which means "one directory higher" and is a good way to get a sibling directory.
$target_dir = "../files/" . usernameIs() . "/";
The above results in a relative path. You can wrap it in realpath() to expand it to a full path.
I will more often use the __DIR__ constant though, to get the directory of the current file and then traverse up with ..
$target_dir = __DIR__ . "/../files/" . usernameIs() . "/";
Finally, $_SERVER['DOCUMENT_ROOT'] is an option as well. In your case, files/ is inside the web server document root, so you may also use:
$target_dir = $_SERVER['DOCUMENT_ROOT'] . "/files/" . usernameIs() . "/";
You might also consider defining a constant that retains the filesystem root of your application, which you can refer to throughout when doing include/require etc.
define("APPLICATION_ROOT", '/home/u875642593/public_html');
There isn't much to gain in your exact situation though, because your project is already at the document root. It does help if you need to move it to a different directory though, like if you kept a dev version in /home/u875642593/public_html/dev.

scandir fail to open directory

I am having trouble with scandir(). I am trying to display the files in my snaps-directory on a page under the subdomain in my cloud.
This is the PHP I used.
$files = scandir('./snaps');
print_r($files);
and this is the error.
Warning: scandir(./snaps) [function.scandir]: failed to open dir: No such file or directory in /home/u703778423/public_html/cloud/index.php on line 39
Warning: scandir() [function.scandir]: (errno 2): No such file or directory in /home/u703778423/public_html/cloud/index.php on line 39
I have no idea what else to do.
You probably assume, that the current work directory is next to the script scandir() is written in, which (in many cases) isn't.
scandir(__DIR__ . '/snaps');
Given that error, your snaps directory would have to have the absolute path of
/home/u703778423/public_html/cloud/snaps
Make sure that this is the correct location for the directory, and that the webserver has the rights to access it.
First off you're trying to display a few files that will not render as images... ex. (.zip).
Also are you making sure you're not trying to display the first two index values " . " and " .. "? I'm thinking this part of your code might not be the issue...

PHP rename directory failed

Well my PHP script generated an error with a hyperlink in it.
Does anyone know what's wrong?
PHP Warning: rename(./uploads/temp/00013/,./uploads/orders/39/) [<a href='function.rename'>function.rename</a>]: No such file or directory
update:
actual code in PHP
if(!file_exists('uploads/orders/')) {
mkdir('uploads/orders/'); // ensuring the orders folder exist
}
rename('uploads/temp/' . $u . '/', 'uploads/orders/' . $i . '/');
update:
Sorry, my fault. I coded to delete previous temp folder before this code execute. Thanks!
It seems that one (or both) of these directories don't exist:
uploads/temp/00013
uploads/orders/39
Have you checked that:
these directories exist?
Apache/PHP has permission to read/write in these directories?
Your current directory is really the parent directory of your "upload" directory?
When a computer tells you
No such file or directory
the first thing you should check is if the file/directory exists. This is not a random error message, it's given only in the specific situation when a file or directory you try to use does not exist.
In this case in particular, both ./uploads/temp/00013/ and ./uploads/orders/ have to exist. If orders doesn't exist it's not created for you.

Categories