Hello
I have been working through Kevin Yanks Book "Database Driven Website"
I have completed the script that allows for a single file upload.
What I do then is insert the image name into a table so I can later pull it out by linking it to the postID of another table.
Anyway here is the script. I need to know how to make it accept and process multiple images. I know it has to do with a foreach function and a array but cant figure it out. I also know that in the html form I need to make them in array format under the name attribute of the input file tage.
Here is the form with the multiple input file tags. It is also used for posting and pulls out authors, but thats besides the point. I just need to learn how to process multiple imags.PHP Code://check for picture upload
if (isset($_FILES['postPIC'])){
//get filename from client machine
//strip file extension from filename
$original_file_name = substr($_FILES['postPIC']['name'],0,-4);
//if no picture was uploaded kill process
if(!is_uploaded_file($_FILES['postPIC']['tmp_name'])){
echo '<p>You selected not to add a picture to this post.</p>';
} else {
//retrieve postID from tbl_blogpost
$blogpostID = mysql_insert_id();
if (eregi('^image/p?jpeg(;.*)?$',
$_FILES['upload']['type'])){
$extension = '.jpg';
} else {
$extension = '.gif';
}
//The complete path/filename create name of file
$filelocation = 'C:/wamp/www/myblog/post_pics/';
$filename = $original_file_name . $postDate . $extension;
$completefile = $filelocation . "" . $filename;
//copy file (if it'deemed safe)
if (is_uploaded_file($_FILES['postPIC']['tmp_name'])and
copy($_FILES['postPIC']['tmp_name'],$completefile)){
echo "<p>Image stored successfully at <br />$completefile</p>";
} else {
echo "<p>Could not save image!</p>";
}
//insert picture info into tbl_postimages
$sql2 = "INSERT INTO tbl_postimages SET
postID='$blogpostID',
image_name='$filename' ";
if (@mysql_query($sql2)){
echo '<p><i>Picture info inserted in DB</i></p>'.
'<a href="viewposts.php">Return </a>';
} else {
echo '<p>Error adding picture info in db: ' .
mysql_error(). '</p>';
}
}
}
PHP Code:<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"
enctype="multipart/form-data">
<h3>Create Blog Entry</h3>
<p>Post Title:</p>
<input type="text" name="postTitle" size="25"/><br />
<p>Author:</p>
<select name="postauthorID" size="1">
<option selected value="">Select One</option>
<option value="">-------</option>
<?php
//Display authors drop down box
while ($authorrow = mysql_fetch_array($authors)){
$aid = $authorrow['authorID'];
$aname = htmlspecialchars($authorrow['authorName']);
echo "<option value='$aid'>$aname</option>\n";
}
?>
</select>
<p>Upload Pictures:</p>
<input type="file" size="35" name="postPIC[]" /><br />
<input type="file" size="35" name="postPIC[]" />
<input type="file" size="35" name="postPIC[]" />
<p>Blog Entry Text:</p>
<div id="postoptions">Formatting Options | You must open and close each tag <br />
Bold = [b] [eb] | example [b]<strong>This is bold text </strong>[eb]<br />
Italics = [i] [ei] | example [i]<em>This is italic text </em>[ei]<br \>
Hyperlink URL = [l] [el] | example [l]http://www.somesite.com/[el]
</div>
<textarea name="postText"></textarea><br /><br />
<input type="submit" value="Post"/>
<input type="reset" value="Reset" />
</form>







I was working through your example trying to apply it to my upload script but it line above throws an error for me. Any chance you could help me out?
no problem

Bookmarks