...

View Full Version : Why will this file upload not work?



RRT
02-28-2012, 08:07 PM
Hi, I am trying to create a php file upload page where JPG images can be uploaded to the server. Below are the 2 php pages that should give this functionality. For some reason it is not working. Do you have any idea as to why this file upload not work?:

upload.php

<?php
echo "<HTML>\n";
echo "<TITLE>Upload a file</TITLE>\n";
echo "<BODY>\n";
echo "<form action=\"do_upload.php\" method=\"post\" enctype=\"multipart/form-data\">\n";
echo "<P><STRONG>File to Upload:</STRONG><BR>\n";
echo "<input type=\"file\" name=\"file\">\n";

echo "<P><input type=\"submit\" name=\"submit\" value=\"Upload image\">\n";
echo "<input type=\"reset\" value=\"reset\" name=\"Reset\">";

echo "</form>";
echo "</body>";
echo "</html>";


do_upload.php:

<?php
$img1_name = $_POST[img1];
$img1_type = $_POST[file];

echo " " . trim($img1) . "\n";
echo " " . trim($img1_name) . "\n";
echo "\n";
echo "<P><HR>";

if (is_uploaded_file($img1_name)) {
move_uploaded_file($img1_name, "/var/www/html/acme/$img1_name") or die("<H3><FONT COLOR=RED>ERROR 2 : (error uploading image file to portal)</H3><HR><FONT COLOR=BLACK> <LI>Couldn't copy the file. Please contact Technical Support.");
}
else {
die("<H3><FONT COLOR=RED>ERROR 1 : (error uploading image file to portal)</H3><HR><FONT COLOR=BLACK> <LI>No input file specified. Select a file first before clicking the <I>Upload File</I> button.");
}
?>

<HTML>
<HEAD>
<TITLE>Upload Status...</TITLE>
</HEAD>
<BODY>
<H1>Success!</H1>
</BODY>
</HTML>


Do you see any syntax or logic errors as to why this isn't working properly? During the 'if-else' block, it's just going to the 'die' section where it says "ERROR 1".

tangoforce
02-28-2012, 10:54 PM
First of all files are stored in the $_FILES array - not $_POST. That is your first error.

Your second error is assuming that move_uploaded_file() was successful and outputting a success message even if it failed. The function returns a boolean result so you can use it in an if() conditional to check and act accordingly.

At the top of your script, do this:
var_dump($_FILES);

That will let you see what is there and how to access it.

Also for your first script, you can turn that into a html page - you don't need to echo it line by line. If you want to echo it via php then you can do this:

echo <<<STOP
<html>
<!--your html here-->
</html>
STOP;

That will make PHP echo out everything until the word STOP

RRT
02-29-2012, 05:24 PM
OK, so you are saying to modify the second file that I posted to this:



<?php
$img1name = $_FILES['img1'];


var_dump($_FILES);
echo "NAME: " . "'%s'" . "\n", pg_escape_string(trim($img1name));
echo "\n";
echo "<P><HR>";

if (is_uploaded_file($img1name)) {
move_uploaded_file($img1name, "/var/www/html/portal/acme/images/$img1_name") or die("<H3><FONT COLOR=RED>ERROR 2 : (error uploading image file to portal)</H3><HR><FONT COLOR=BLACK> <LI>Couldn't copy the file. Please contact Technical Support.");
}
else {
die("<H3><FONT COLOR=RED>ERROR 1 : (error uploading image file to portal)</H3><HR><FONT COLOR=BLACK> <LI>No input file specified. Select a file first before clicking the <I>Upload File</I> button.");
}
?>


This is the result that I get from this new code change:



array(1) { ["img1"]=> array(5) { ["name"]=> string(10) "Winter.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(14) "/tmp/php1rEx6y" ["error"]=> int(0) ["size"]=> int(105542) } } NAME: '%s' Array

ERROR 1 : (error uploading image file to portal)
No input file specified. Select a file first before clicking the Upload File button.

tangoforce
02-29-2012, 05:36 PM
You should be accessing the file like this:
$img1name = $_FILES['img1']['tmp_name'];

Let me explain how this all works..

When you upload a file to a php script, php stores its details in the $_FILES array whilst storing the file as a temporary file with a temporary name. In the files array you will find a multidimensional array which will contain the temp name, the file size, the real name etc.

When accessing the uploaded file, you need to address it by its temporary name until you move it to its new location and save it under its original (or any other) file name.

Currently you're doing this:
$img1name = $_FILES['img1'];

Which will basically set $img1name to an array of the files details. You can't then move an array using move_uploaded_file() to its new location - an array is a chunk of memory not a file!

This is something like what you should be doing:
isset($_FILES)
!empty($_FILES)
is_uploaded_file($_FILES['img1']['tmp_name'])
move_uploaded_file($_FILES['img1']['tmp_name'], $_FILES['img1']['name'])

RRT
03-02-2012, 12:54 AM
Wow, your insight has been very helpful, I *really* appreciate it! One quick follow-up question: if I want the 'move_uploaded_file' function to be able to move the file being uploaded to a sub-directory or some other directory, how would I specify that within the syntax:


move_uploaded_file($_FILES['img1']['tmp_name'], $_FILES['img1']['name'])

tangoforce
03-02-2012, 06:22 PM
Something like this:


move_uploaded_file($_FILES['img1']['tmp_name'], 'some_other_directory/' .$_FILES['img1']['name']);


Or:

move_uploaded_file($_FILES['img1']['tmp_name'], "some_other_directory/$_FILES[img1][name]");

RRT
03-02-2012, 07:49 PM
AWESOME, it worked! You rock!!! Thanks again.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum