PHP mkdir failing without errors - php

I've am trying to set up a small file sharing server on my home's local network and am running into some big problems with the uploader. the step it seems to be failing on is the directory creation step however, when I first posted this there were no errors in the Apache log files however, that turned out to be the result of a permission problem with the lag files.
this are the relevant log entries.
[Mon Mar 25 18:43:05 2013] [error] [client 10.0.0.17] PHP Warning: mkdir(): Permission denied in /server/upload_movie.php on line 10, referer: http://10.0.0.17/upload_movie.html
it confuses me because I have run
sudo chmod 0777 /server/*
sudo chmod 0777 /server
with /server/ being the rood directory.
my code is as follows
<?php
echo "starting". "<br>";
$allowedExts = array("mp4", "mpg", "avi", "mkv");
$extension = end(explode(".", $_FILES["uplodedfile"]["name"]));
echo "filetype parsed". "<br>";
$path = "/downloads/movies/unsorted/";
echo "checking upload directory". "<br>";
if(!is_dir($path)){
echo "upload directory not found, creating...";
if (mkdir($path,0777,true))
{
echo "directory creation complete". "<br>";
}
else
{
echo "directory creation failed at ".$path."<br>";
}
}
echo "checking file". "<br>";
if (false)
{
echo "filetype and size passed". "<br>";
if ($_FILES["uplodedfile"]["error"] > 0)
{
header('Location: upload_failure.php?file='.$_FILES["uplodedfile"]['name'].'&error='.$_FILES["uplodedfile"]["error"]);
exit();
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["uplodedfile"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists on server. ". "<br>";
}
else
{
echo "creating perminant copy of file". "<br>";
move_uploaded_file($_FILES["uplodedfile"]["tmp_name"],
$path."/" . $_FILES["uplodedfile"]["name"]);
echo "Stored in: " . "movie_uploads/" . $_FILES["file"]["name"]. "<br>";
}
}
header('Location: upload_success.php?type=movie');
exit();
}
else
{
echo "error:<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Name: " . $_FILES["file"]["name"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
echo "extension: ".$extension;
exit();
// echo "Invalid file";
}
?>
the output is as follows
starting
filetype parsed
checking upload directory
upload directory not found, creating...directory creation failed at /downloads/movies/unsorted/
checking file
error:
Type:
Name:
Size: 0 kB
Temp file:
extension:
and the code calling it is
<form enctype="multipart/form-data" action="upload_movie.php" method="POST">
<input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
i have tried all the suggestions in PHP mkdir: Permission denied problem and I have selinux turned off. I am using fedora 17. the server is being run on an ext4 partition which contains nothing else.
as was suggested in comments, i tried
$error = error_get_last(); echo $error['message'];
which yealded
Undefined index: file

You seem to have set the correct permissions to the /server-directory. But your server wants to write to the /downloads- directory. You should be up and running with this command:
Sudo chmod 777 /download

Your error message tells everything in it. This is file write permission problem. If you want to write your file in /downloads/movies/unsorted/ then you need to set write permission to this directory for all (777). But you are saying, you already checked permission for the downloads directory, but you might not set permission for recurring directories. So try below command before uploading file.
Sudo chmod -R 777 /download

Related

No temp file in MAMP whilst trying to upload - no file found [duplicate]

So I have 4 pages. They are very simple.
index.php (WORKS)
<html>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br />
<input type="submit" value="Now upload it!">
</form>
</html>
upload.php (WORKS)
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
echo 'Are you sure you want to continue saving this file';
echo 'Yes, continue
<br />
<br />
No thanks'
}
?>
no.php (WORKS)
<?php
echo 'Thanks anyway';
?>
yes.php (ERROR)
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
echo 'We will now save this document:';
//Save document code
}
?>
Output of yes:
Notice: Undefined index: file in /home/public_html/test/yes.php on line 2 Invalid file
We will now save this document:?
As you can see I never save it. But I would like to save it in the yes.php page. Is it still possible to retrieve that original doc that was uploaded? Thanks in advance.
Uploaded files are only available for a single PHP instance/request cycle.
Uploaded files are stored in the temp directory. If they're still there when the script finishes executing, PHP will delete them assuming you didn't need them.
If you want to persist the file, you'll have to move it elsewhere in the same request that the file was uploaded.
You are trying to pass files from a form to a different page then intended. The post values will no longer be valid. I would suggest saving the file in the upload.php to a temporary folder and from there passing it to either the yes or no page via a $_GET[] or session variable.
On the no.php page you would take that file and use
unlink($somefile);
This will delete the file from your server.
On the yes.php page I would move or copy the file. If you copy the file, I would use unlink to remove the temp file.
You could try using move_uploaded_file() to temporarily save the file and then pass the file information using
Yes, continue
No thanks
Then retrieve the filename in yes.php using
$tmpFile = $_GET["filename"];
or remove it in no.php using
unlink($_GET["filename"]);
If the file is not too large, copy the file content in a session variable. The session will be preserved from one page to another.

Moving Files with PHP that have been uploaded

I've gone through some of the questions, but haven't found an answer to my question so here it goes.
I've written a stereotypical script for uploading small files. The script works, and the file is uploaded to the server. However, I can't get it to upload it to move to the right subfolder.
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " MB<br>";
}
if (file_exists("/enhstudios/clients/media/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"/enhstudios/clients/media/" . $_FILES["file"]["name"]);
}
?>
The error I get is:
Warning: move_uploaded_file(/enhstudios/clients/july.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /hermes/waloraweb013/b1311/moo.enhstudios/enhstudios/clients/fileuploadcode.php on line 20
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpkfxGLm' to '/enhstudios/clients/july.jpg' in /hermes/waloraweb013/b1311/moo.enhstudios/enhstudios/clients/fileuploadcode.php on line 20
Permissions for this directory are set at 777.
Where have I gone wrong? I've tried all different combos of subdirectories and still can't get it to upload to the right one.
Are you sure you don't mix the /hermes/waloraweb013/b1311/moo.enhstudios/enhstudios/clients and the /enhstudios/clients/ directories?
I think you would like to move to the longer one, but the second arguments first / makes it an absoulte path.
You can add there the full path, or you can try to remove the first /. (This relativisation works only if the document root is /hermes/waloraweb013/b1311/moo.enhstudios/)

file uploading in php

Here my code
echo "Upload: " . $_FILES["audio"]["name"] . "<br/>";
echo "Type: " . $_FILES["audio"]["type"] . "<br/>";
echo "Size: " . ($_FILES["audio"]["size"] / 1024) . " kB<br/>";
echo "Temp file: " . $_FILES["audio"]["tmp_name"] . "<br/>";
move_uploaded_file($_FILES["audio"]["tmp_name"],
"/var/www/html/mnworld/upload/audiofile/" . $_FILES["audio"]["name"]);
echo "Stored in: " . "/var/www/html/mnworld/upload/audiofile/" . $_FILES["audio"]["name"];
I tried to upload file in this (/var/www/html/mnworld/upload/audiofile/) directory but its always empty its not showing any errors. I need to upload this directory
Check the permission of the folder where the files are to be stored is in 777 mode (both Read and write). If not, change the mode to chmod 777. And also make sure that the path to be stored should be correct.
Since you have the correct details. The problem is only the permission of the folder that unables you to move/copy the file.
Use this: chmod 777 /var/www/html/mnworld/upload/audiofile/

Take file upload and go to another PHP page

So I have 4 pages. They are very simple.
index.php (WORKS)
<html>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br />
<input type="submit" value="Now upload it!">
</form>
</html>
upload.php (WORKS)
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
echo 'Are you sure you want to continue saving this file';
echo 'Yes, continue
<br />
<br />
No thanks'
}
?>
no.php (WORKS)
<?php
echo 'Thanks anyway';
?>
yes.php (ERROR)
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
echo 'We will now save this document:';
//Save document code
}
?>
Output of yes:
Notice: Undefined index: file in /home/public_html/test/yes.php on line 2 Invalid file
We will now save this document:?
As you can see I never save it. But I would like to save it in the yes.php page. Is it still possible to retrieve that original doc that was uploaded? Thanks in advance.
Uploaded files are only available for a single PHP instance/request cycle.
Uploaded files are stored in the temp directory. If they're still there when the script finishes executing, PHP will delete them assuming you didn't need them.
If you want to persist the file, you'll have to move it elsewhere in the same request that the file was uploaded.
You are trying to pass files from a form to a different page then intended. The post values will no longer be valid. I would suggest saving the file in the upload.php to a temporary folder and from there passing it to either the yes or no page via a $_GET[] or session variable.
On the no.php page you would take that file and use
unlink($somefile);
This will delete the file from your server.
On the yes.php page I would move or copy the file. If you copy the file, I would use unlink to remove the temp file.
You could try using move_uploaded_file() to temporarily save the file and then pass the file information using
Yes, continue
No thanks
Then retrieve the filename in yes.php using
$tmpFile = $_GET["filename"];
or remove it in no.php using
unlink($_GET["filename"]);
If the file is not too large, copy the file content in a session variable. The session will be preserved from one page to another.

Can't find uploaded files after PHP image upload

I'm uploading files from an iPhone app to PHP using the following code:
<?php
if ($_FILES["media"]["error"] > 0) {
echo "Error: " . $_FILES["media"]["error"] . "<br />";
} else {
echo "Upload: " . $_FILES["media"]["name"];
echo "Type: " . $_FILES["media"]["type"];
echo "Size: " . ($_FILES["media"]["size"] / 1024);
echo "Stored in: " . $_FILES["media"]["tmp_name"];
if (file_exists("uploads/" . $_FILES["media"]["name"])) {
echo $_FILES["media"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["media"]["tmp_name"],
"uploads/" . $_FILES["media"]["name"]);
echo "Stored in: " . "uploads/" . $_FILES["media"]["name"];
}
}
?>
Afterwards, I get the success message saying "uploads/2542543.jpg" - but I can't seem to find where the image is actually stored. Is the filepath relative to the php file (which is in php - and has an uploads folder) or is it absolute from the root??
EDIT: Looks like the file should have ended up in php/uploads/filename.jpg - but it doesn't appear to be making it. I don't quite understand why - anyone have any idea?
This is an absolute path from the root
/uploads/2543.jpg
This is a relative path from your PHP document
uploads/2543.jpg
So you're working with a relative path (note the missing / at the start)
This is defined in php.ini using the upload_tmp_dir directive. If that's not set, it uses the system default, which you can figure out using sys_get_temp_dir. I believe it defaults to /tmp on Linux, and C:\Windows\Temp\ on Windows.

Categories