?
Solved

file uploading with move_uploaded_file()

Posted on 2004-03-24
4
Medium Priority
?
325 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 6

Accepted Solution

by:
Andy earned 300 total points
ID: 10673266
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 300 total points
ID: 10673302
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 300 total points
ID: 10675180
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
ID: 10771146
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

800 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