Forum Moderators: coopster

Message Too Old, No Replies

file upload

         

jackvull

4:03 pm on Jan 16, 2006 (gmt 0)

10+ Year Member



Hi
I have to following HTML form to upload a file:
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
<input name="userfile[]" type="file" />
<input type="submit" value="Submit" />
</form>

In my upload script I have:
if(is_uploaded_file($_FILES['userfile']['tmp_name']))
{
..do some code, etc.
}

There seems to be nothing in the $_FILES array after selecting a file and clicking submit. Any ideas why?
All I get is this when trying to echo some of the $_FILES variables:
File Array is Array bytes, i.e. the word Array is just being printed instead of the relevant info.

dreamcatcher

5:48 pm on Jan 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try using:

print_r($_FILES);

To see what data is coming in.

dc

jackvull

9:46 am on Jan 17, 2006 (gmt 0)

10+ Year Member



Hi
I get the following:

Array ( [userfile] => Array ( [name] => Array ( [0] => headphones.gif ) [type] => Array ( [0] => image/gif ) [tmp_name] => Array ( [0] => C:\PHP\uploadtemp\php4D2.tmp ) [error] => Array ( [0] => 0 ) [size] => Array ( [0] => 1058 ) ) )

jackvull

10:03 am on Jan 17, 2006 (gmt 0)

10+ Year Member



This is the code for the upload function in case you were wondering:

<?
// the upload function
function upload()
{

$maxsize = 1000000;
print_r($_FILES);

if(is_uploaded_file($_FILES['userfile']['tmp_name']))
{

// check the file is less than the maximum file size
if($_FILES['userfile']['size'] < $maxsize)
{
// prepare the image for insertion
$imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));
// $imgData = addslashes($_FILES['userfile']);

// get the image info..
$size = getimagesize($_FILES['userfile']['tmp_name']);

// put the image in the db...
$sql = "INSERT INTO Tracks (
TrackID, Artist, Title, Mix, LabelID, GenreID, ReleaseDate, Picture,
192Path, 300Path, WavPath, SamplePath, Price, PictureSize, Image'name' )
VALUES (
1, '', '', '', 1, 1, now(), '{$imgData}', '', '', '', '', 0.80,
'{$size[3]}', '{$_FILES['userfile']['name']}' )";

// insert the image
if(!mysql_query($sql))
{
echo 'Unable to upload file';
}
}

else
{
// if the file is not less than the maximum allowed, print an error
echo "
<div>File exceeds the Maximum File limit</div>
<div>Maximum File limit is ".$maxsize."</div>
<div>File ".$_FILES['userfile']['name']." is ".$_FILES['userfile']['size']." bytes</div>
<hr />";
}
}

else
{
echo "
<div>File wasn't uploaded</div>
<hr />";
}
}
?>

vevs

10:31 am on Jan 17, 2006 (gmt 0)

10+ Year Member



it should be
<input name="userfile" type="file" />
not
<input name="userfile[]" type="file" />

Regards,
Vevs

dreamcatcher

10:54 am on Jan 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yep, Vevs is right. You are creating an array which you aren`t accessing. ie, you are only uploading 1 file, not multiple ones.

dc

jackvull

10:58 am on Jan 17, 2006 (gmt 0)

10+ Year Member



Perfect. Thanks.

jackvull

12:36 pm on Jan 17, 2006 (gmt 0)

10+ Year Member



Right, next issue.
I am inserting the picture into a DB BLOB column.
When I try to retrieve the picture all I get is a load of computer junk like:
GIF89a÷™GIF89a–•w!ù™,–•‡!!!!!)!!!!)!)!))!)

Is there some sort of function that I need to use to convetr the file back into an image or some sort of header?

wheelie34

2:50 pm on Jan 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am new to php too but after reading the posts thought maybe do you still have print_r($_FILES); in your code? from DC's post, wouldnt that read the gif rather than display it?

coopster

2:52 pm on Jan 17, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



A related thread that may be of interest:
How to convert image blob files back to image [webmasterworld.com]

jackvull

3:04 pm on Jan 17, 2006 (gmt 0)

10+ Year Member



This is what I am doing and it still uotputs the binary data:
$sql = "SELECT Picture, TrackID, Artist, Title
FROM Tracks
";
$result = mysql_query($sql) or trigger_error(mysql_error());
while ($row = mysql_fetch_assoc($result))
{
$image = $row['Picture'];
header("Content-type: image/gif");
print $image;
}

jackvull

10:55 pm on Jan 17, 2006 (gmt 0)

10+ Year Member



Any ideas why that might not work?
Thanks.

coopster

3:31 pm on Jan 18, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yeah, you are looping and printing a header() over and over again. Don't use a while loop, just get the image and print it if that is what you want.