Solved

Help debug my upload.php

Posted on 2010-11-16
15
258 Views
Last Modified: 2012-05-10
Hi, Yes this is a demo from http://www.tizag.com/phpT/fileupload.php

however it does not work, it comes up as echo not writeable even tho my file is on 777 persmissions.

anyideas would be appricated.

Cheers.

it comes up with not writeable :S
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>


///////////////////////Upload code/////////////

<?
// Where the file is going to be placed 
$target_path = "uploads/";

/* Add the original filename to our target path.  
Result is "uploads/filename.extension" */
$target_path = "./uploads/".basename($_FILES['uploadedfile']['name']);
 
if (is_writable("./uploads/")) {
    echo "Is Writable";
    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path);
} else {
    echo "Is not Writable";
}
 

?>

Open in new window

0
Comment
Question by:NeoAshura
  • 5
  • 4
  • 3
  • +1
15 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 167 total points
ID: 34146463
"Not writeable" is a permissions error, not a PHP script error.

The folder you are trying to write to will not let the webdaemon have access to create a new file. Either change the group ownership of the folder or (simplest of all) set the permissions for the folder to 777 to allow universal access.
0
 
LVL 6

Author Comment

by:NeoAshura
ID: 34146491
i set the access to 777 as i started in my opening comment. still no luck. I have got a file named uploads in my web root and also in the folder where my uploadform is stored. still no joy.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34146613
Sorry - I missed the 777 comment in your original post.

For folder the execute bit (the x in rwx) needs to be set so if the parent folder of the folder you are trying to write to has no "x" bit set then the folder in question will not be accessible.

Next is the folder where you think it is? Are you sure that the ./uploads folder is where you expect it to be? Try altering the script to look like this (see http://www.php.net/realpath )

echo "Attempting write to " . realpath( "./uploads" );
 
if (is_writable("./uploads/")) {
    echo "Is Writable";
    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path);
} else {
    echo "Is not Writable";
}
 

Next, check spelling make sure your folder is "uploads" and not "upload" or "Upload" or "Uploads", etc.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 3

Assisted Solution

by:Prograministrator
Prograministrator earned 167 total points
ID: 34146633
Hello,

is_writable returns that "upload" folder not writable,

change the permission of this folder!

if its permissions already 777,

so check the php safe mode of change your condition to another function than is_writable
0
 
LVL 3

Expert Comment

by:Prograministrator
ID: 34146745
Sorry, for typo

"so check the php safe mode, or change your condition to another function than is_writable".

Pay attention, you should to change the permissions of the folder that you're uploading to it,

not permissions of the php file or the file that you're uploading, and not the permissions of the folder that you're uploading from.

"uploads" folder's permissions is that should to be changed.

Good luck.
0
 
LVL 6

Author Comment

by:NeoAshura
ID: 34146755
ok now it coming up as IT IS WRITEABLE but its not uploading :S im so confused. its 777 it says now its writeable but still doesnt upload my file (just a standard 2kb text file)
0
 
LVL 13

Assisted Solution

by:dsmile
dsmile earned 166 total points
ID: 34146768
Two things:
1. is "./uploads" path correct (relatively to your script)?
2. try to call clearstatcache() before checking writing permission
0
 
LVL 13

Expert Comment

by:dsmile
ID: 34146801
Try to use this

$target_path = "./uploads/".$_FILES['uploadedfile']['name'];


instead of

$target_path = "./uploads/".basename($_FILES['uploadedfile']['name']);
0
 
LVL 6

Author Comment

by:NeoAshura
ID: 34146826
okk what ive got now is as follows..

<?
error_reporting(E_ALL);
// Where the file is going to be placed
$target_path = "uploads";

/* Add the original filename to our target path.  
Result is "uploads/filename.extension" */
$target_path = "./uploads".basename($_FILES['uploadedfile']['name']);
 
 echo "Attempting write to " . realpath( "./uploads" );

 
if (is_writable("./uploads")) {
    echo "Is Writable";
    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path);
} else {
    echo "Is not Writable";
}
 

?>


and the output is...of the echo is as follows :

Attempting write to (correct path)/uploadsIs Writable
0
 
LVL 3

Expert Comment

by:Prograministrator
ID: 34146908
What is the size of the file you're attempting to upload?

check this values in your php.ini :

post_max_size
upload_max_filesize
memory_limit


0
 
LVL 6

Author Comment

by:NeoAshura
ID: 34146955
its 2kb so shouldnt be a prblem.
0
 
LVL 13

Expert Comment

by:dsmile
ID: 34147011
Your latest code is missing a slash in $target_path

$target_path = "./uploads".basename($_FILES['uploadedfile']['name']);

Should be

$target_path = "./uploads/".basename($_FILES['uploadedfile']['name']);

And I don't think basename is necessary, so this is enough

$target_path = "./uploads/".$_FILES['uploadedfile']['name'];
0
 
LVL 6

Author Comment

by:NeoAshura
ID: 34147013
ok problem solved, i just did another tutorial from a diffrent site and it worked, i appricate peoples time so points will be awarded fairly. thanks.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34147066
Place alter this

error_reporting(E_ALL);

to this

ini_set("display_errors", 1);   error_reporting(E_ALL);

in display_errors is OFF - this would prevent error_reporting from showing anything. Also change the following code

if (is_writable("./uploads")) {
    echo "Is Writable";
    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path);
} else {
    echo "Is not Writable";
}

to report the statement it is about to run (like so)

if (is_writable("./uploads")) {
    echo "Is Writable";
    echo "move_uploaded_file({$_FILES['uploadedfile']['tmp_name']}, $target_path)");
    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path);

exit;  //   <--- get rid of this after testing

} else {
    echo "Is not Writable";
}


You will notice that it contains an exit statement to stop execution immediately after the move. This is to ensure that nothing else interferes until we are certain that we know the upload works.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34147077
LOL! I see I need to type faster.

;-)

Glad you're sorted
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question