File Uploads: File Isn't Set

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
captcadaver
Forum Newbie
Posts: 17
Joined: Fri Jul 17, 2009 11:12 pm

File Uploads: File Isn't Set

Post by captcadaver »

Hey, I'm new here. I'm working on a photo gallery project for a student group so I can learn PHP and about server-side scripting. This particular section has been giving me nothing but trouble.

My current problem: I think my problem is that my files aren't set. I don't think they're posted correctly.

So, here is the first part of the upload process: upload_pic.php.

<?php include 'config.inc.php'; ?>

<html>

<head>
<title>Upload Pictures to Album</title>
</head>

<body>

<form enctype="multipart/form-data" action="describe_pic.php" method="POST">
<h1>Upload Pictures to Album</h1>


<br / >
Select Album:
<select name="album">
<?php
$result_album = mysql_query('SELECT * FROM photo_album') or die(mysql_error());
while($row_album = mysql_fetch_array($result_album))
{
echo "<option value=".$row_album['aid'].">".$row_album['title']."</option>";
}
?>
</select>
<br / >
<br / >

Browse for Photos:
<br / >
<input type="hidden" name="MAX_FILE_SIZE" value="5242880" />
<input name="pic1" type="file" /><br />
<input name="pic2" type="file" /><br />
<input name="pic3" type="file" /><br />
<input name="pic4" type="file" /><br />
<input name="pic5" type="file" /><br />
Size Limit: 5 MB/picture
<br / >
Accepted Picture Formats: JPG
<br / >
<br / >

<input type="submit" value="Submit" name="submit"/>

</form>

</body>
</html>

Here is the second and incomplete part of the chain, describe_pic.php. I've only pasted the top part. How can I post files? Apparently they aren't being "set":

<?php include 'config.inc.php'; ?>

<html>

<head>
<title>Describe Pictures</title>
</head>

<body>

<?php
// script stolen from event pic uploader
$details .= "> Processing Images...<br/>";

// get aid
$album = $_POST['album'];
$sql = "SELECT aid FROM photo_album WHERE title='$album'";
$aid = mysql_query($sql) or die(mysql_error());

// get gid associated w/ aid
$gid = mysql_query("SELECT gid FROM photo_album WHERE aid='$aid'") or die(mysql_error());

// find out which upload slots had pictures in them
$set_pic_array = array(); // store set pics here
$accepted_pic_array = array(); // store accepted pics here. will be used later
//for ($i=1; $i<=5; $i++)
// {
// if (isset($_FILE['pic'.$i]))
// array_push($set_pic_array, $_FILE['pic'.$i]); // if slot contained a photo, add it to the $set_pic_array
// }

// DEBUG
echo "aid: $aid";
echo "<br / >";
echo "gid: $gid";
echo "<br / >";
//foreach($set_pic_array as $contents)
// {
// echo "$contents";
// }
echo "<br / >";
$pic_single = 'pic1';
$set = var_dump(isset($_FILE[$pic_single])); // is pic1 set?
echo "set or no? $set";
echo "<br / >";
//

// check each pic's validity
// foreach ($set_pic_array as $pic_single) {
$results = mysql_query("SELECT * FROM photo_pic WHERE aid='$aid'") or die(mysql_error()); // get info about this pic's album AND help generate new pid
$pid = mysql_num_rows($results) or die(mysql_error()); // assign pid based on # entries in album

//DEBUG
echo "pid: $pid";

Thanks.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: File Uploads: File Isn't Set

Post by requinix »

I think you'll have better luck using the $_FILES array.
captcadaver
Forum Newbie
Posts: 17
Joined: Fri Jul 17, 2009 11:12 pm

Re: File Uploads: File Isn't Set

Post by captcadaver »

Awesome, thanks.

Can you guys help me figure out to fix this "resource id #" business when I try to echo and use certain table data? I've tried using mysql_fetch_assoc() on the query results but then nothing shows up and the variable isn't set.

Code:

<?php
// script stolen from event pic uploader
$details .= "> Processing Images...<br/>";

// get aid
$album = $_POST['album'];
$sql = "SELECT aid FROM photo_album WHERE title='$album'";
$result = mysql_query($sql) or die(mysql_error());
//while ($row = mysql_fetch_assoc($result))
// {
// $aid = $row['aid'];
// }

// get gid associated w/ aid
$gid = mysql_query("SELECT gid FROM photo_album WHERE aid='$aid'") or die(mysql_error());

// find out which upload slots had pictures in them
$set_pic_array = array(); // store set pics here
$accepted_pic_array = array(); // store accepted pics here. will be used later
//for ($i=1; $i<=5; $i++)
// {
// if (isset($_FILE['pic'.$i]))
// array_push($set_pic_array, $_FILE['pic'.$i]); // if slot contained a photo, add it to the $set_pic_array
// }

// DEBUG
echo "aid: $aid";
echo "<br / >";
echo "gid: $gid";
echo "<br / >";
$aid_set = var_dump(isset($aid));
echo "is aid set? $aid_set";
//foreach($set_pic_array as $contents)
// {
// echo "$contents";
// }
echo "<br / >";
$pic_single = 'pic1';
$set = var_dump(isset($_FILES[$pic_single])); // is pic1 set?
echo "pic1 set or no? $set";
echo "<br / >";
//

?>

After trying to upload a pic (most of the upload code I didn't include):

aid:
gid: Resource id #4
bool(false) is aid set?
bool(true) pic1 set or no?
line 58: T
Warning: move_uploaded_file(uploads/Resource id #4//arrested-development-season-3.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/nobenati/public_html/photos/describe_pic.php on line 73

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/var/tmp/phpeZ4YxZ' to 'uploads/Resource id #4//arrested-development-season-3.jpg' in /home/nobenati/public_html/photos/describe_pic.php on line 73
Describe Pictures
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: File Uploads: File Isn't Set

Post by jackpf »

First of all, you're giving gid the query, which is a php resource handle. You need to use something like mysql_fetch_array to fetch data from the database.

Secondly, it looks like you're using $_FILE instead of $_FILES. If you turn up error reporting you should get some notice about an unset variable $_FILE.
Post Reply