Solved

file uploading with move_uploaded_file()

Posted on 2004-03-24
4
304 Views
Last Modified: 2009-07-29
Hello,

I just recently moved my web site to a new server. The new server does not support passing variables from page to page automatically. I now have to modify my scripts to pull in the variables using $_POST and  $_GET (something i've never had to do, what a pain in the @$$!!!)

Before, I was able to use:   copy("$pic", "/dir/tmp/blah/www/$pic_name");  (so easy!!)
But now I'm having a hell of a time trying to get it to work with $_FILES.

I've been told that I can't get the "full path" of the file on the user's computer with $_FILES, IE: "c:/blah/blah/blah/filename.gif" .. because it makes a security hole? Seems like if I could get the full path, like i used to, I could just use copy() again and everything would work fine, but no!! hehe.

So.... I've read the manual at php for "handling file uploads" ..... From what I've read, instead of using copy(), I now have to use move_uploaded_file()... something like this:

$uploaddir = '/my/unix/server/address/www/';
$uploadfile = $uploaddir . $_FILES['pic']['name'];
move_uploaded_file($_FILES['pic']['tmp_name'], $uploadfile);

But that doesn't seem to work. What am I missing? Thanks for any help in advance.


0
Comment
Question by:thaistixxx
4 Comments
 
LVL 6

Accepted Solution

by:
Reapz earned 100 total points
Comment Utility
You dont need to use your full root path just the path relative to the page that's running this code for example...

$uploaddir = 'files/uploads/';
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir . $_FILES['userfile']['name']);

This works fine for me but I also have the following code to display the appropriate error message which may help you id the problem. With regard to error 2, you should include a hidden field in the form called MAX_FILE_SIZE and set the value to the max size in bytes you will allow.

<?php
if($_FILES['userfile']['error'] == "0"){
?>
The image has been uploaded successfully!
<?php
}
else if($_FILES['userfile']['error'] == "1"){
?>
Filesize exceeds the maxmium allowed by the server!<br>
You can contact your server admin and ask to have the 'upload_max_filesize' directive
in the 'php.ini' file increased.
<?php
}
else if($_FILES['userfile']['error'] == "2"){
?>
Filesize exceeds the maxmium allowed by the webmaster!<br>
The current maximum is 100000 bytes. You can contact the webmaster and ask
to have this value increased.
<?php
}
else if($_FILES['userfile']['error'] == "3"){
?>
Due to an unknown error the file was only partially uploaded!<br>
Please try again.
<?php
}
else if($_FILES['userfile']['error'] == "4"){
?>
Due to an unknown error the file has not been uploaded!<br>
Please try again.
<?php
}
?>
0
 
LVL 5

Assisted Solution

by:winglis4
winglis4 earned 100 total points
Comment Utility
Try changing this line:

$uploadfile = $uploaddir . $_FILES['pic']['name'];

to this

$uploadfile = $uploaddir .'/'. $_FILES['pic']['name'];

Hope this fixes it for ya!
0
 
LVL 1

Assisted Solution

by:robinet02
robinet02 earned 100 total points
Comment Utility
Maybe use this :

$uploaddir = '/my/unix/server/address/www/';
$uploadfile = $uploaddir . $_FILES['pic']['name'];

$fp = fopen($uploadfile, "wb");
if ($fp) {
     $content = file_get_contents($_FILES['pic']['tmp_name']);
     $bytes_written = fwrite($fp, $content);
} else {
     print ("open destination failed");
}
if ($bytes_written != strlen($content)) {
     print ("bytes written error");
}

0
 

Author Comment

by:thaistixxx
Comment Utility
I think there was just a missing variable in my script somewhere that was keeping it from working. It was one of those things that you give up on, come back later and fix it in 2 seconds.

Even though none of the answers here really fixed my problem (I fixed it on my own somehow) I still want to split the points up because I really appreciate the time you guys spend on answering questions here and I know sometimes it ends up being a waste to you. Anyway, Thanks!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now