Adastra Posted November 24, 2006 Share Posted November 24, 2006 I have a small script to upload a previously chosen number of photos from 1-10 - but once I want to add the files (upload, resize and write database entries), I get this very very long site full of errors. I'm a PHP newbie, and I don't really know how to use the foreach and arrays and that stuff in this case. I've looked at the database after trying to post photos - it created a new row for each file I tried to upload, but it didn't enter any data, is just wrote the word "Array" into each fields (except the ID). So I assume that the information in the arrays is not correctly passed on... ?!?this is my code: [code]<?phpinclude('config.php');echo $header;if ($_POST['action'] == "add") { foreach ($_POST['title'] as $title) { //__ upload & resize _______________________________________ $ul_photo = $_FILES['photo']['tmp_name']; $src = imagecreatefromjpeg($ul_photo); list($width,$height) = getimagesize($ul_photo); $newwidth = 550; $newheight = ($height/$width)*$newwidth; $tmp = imagecreatetruecolor($newwidth,$newheight); imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); $filename = $dirpath.$gal_id."/". $_FILES['photo']['name']; imagejpeg($tmp,$filename,100); #__ thumb _________________________________________________________ $ul_thumb = $_FILES['photo']['tmp_name']; $srcthumb = imagecreatefromjpeg($ul_thumb); list($widththumb,$heightthumb) = getimagesize($ul_thumb); $newwidththumb = 100; $newheightthumb = ($heightthumb/$widththumb)*$newwidththumb; $src_top = ($src_height / 2) - ($dst_height / 2); $src_left = ($src_width / 2) - ($dst_width / 2); $tmpthumb = imagecreatetruecolor($newwidththumb,$newheightthumb); imagecopyresampled($tmpthumb,$srcthumb,0,0,$src_top,$src_left,$newwidththumb,$newheightthumb,$widththumb,$heightthumb); $thumbname = $dirpath.$gal_id."/zth_".$_FILES['photo']['name']; imagejpeg($tmpthumb,$thumbname,100); #___________________________________________________________________ imagedestroy($src); imagedestroy($tmp); imagedestroy($srcthumb); imagedestroy($tmpthumb); $gal = addslashes($_POST['gal_id']); $photo = $_FILES['photo']['name']; $title = addslashes($_POST['title']); $description = addslashes($_POST['description']); $sql = "INSERT INTO $gal_photos (gal,photo,title,description) VALUES ('$gal','$photo','$title','$description')"; $query = mysql_query($sql) or die("MySQL Error: <br /> {$sql} <br />". mysql_error()); $num = mysql_affected_rows(); if($num > 0) { echo "<span class='result'>New Photo successfully added!</span>"; echo "<br /><img src='../galleries/".$gal."/zth_".$photo."' />"; } else { echo "Error: ".mysql_error(); } } #foreach} elseif ($_GET['action'] == "listforms") {?><strong>Post New Photos</strong><br /><br /><form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data"><input type="hidden" name="MAX_FILE_SIZE" value="649760" /> <? for ($x=0;$x<$_GET['num'];$x++) { ?> <div class='uploadbox'> Associated Gallery <?=$x + 1?>: <br /> <? $selgal_sql = "SELECT * FROM $gal_galleries ORDER BY id DESC"; $selgal_result = mysql_query($selgal_sql) or print ("Unable to select data.<br />".$selgal_sql."<br />".mysql_error()); $selgal_num = mysql_num_rows($selgal_result); if ($selgal_num > 0) { echo "<select name='gal_id[]'>"; while ($selgal_row = mysql_fetch_array($selgal_result)) { $selgal_id = $selgal_row["id"]; $selgal_title = $selgal_row["title"]; echo "<option value='".$selgal_id."'>".$selgal_id.": ".$selgal_title."</option>"; } echo "</select>"; } else { echo "<a href='gal_galleries.php'>Please create a new gallery to post photos!</a>"; } ?> <br /> <br /> Photo File <?=$x + 1?>: <br /> <input type='file' name='photo[]' size='50' /> <input type="hidden" name="thumb[]" value="<? ($_POST['photo']) ?>" /> <br /> <br /> <? if ($usetitle == "n") { ?> <input type='hidden' name='title[]' value='' /> <? } else { ?> Photo Title <?=$x + 1?>: <br /> <input type='text' name='title[]' size='50' /> <br /> <br /> <? } ?> <? if ($usedesc == "n") { ?> <input type='hidden' name='description[]' value='' /> <? } else { ?> Photo Description <?=$x + 1?>: <br /> <textarea cols='45' rows='5' name='description[]'></textarea> <? } ?> </div> <br /> <? } ?> <br /> <br /> <input type="submit" name="action" value="add" /></form><? } else {?> <strong>Post New Photos</strong> <br /> <br /> <form action="<?=$_SERVER['PHP_SELF']?>" method="get"> <input type="hidden" name="action" value="listforms" /> <label for="num">Select number of photos to post</label>: <select id="num" name="num"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> </select> <input type="submit" value="Proceed" /> </form><?}echo $footer;?>[/code]these are my errors: [quote]Warning: imagecreatefromjpeg(Array) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in /htdocs/gall/gal_postphotos.php on line 11Warning: getimagesize(Array) [function.getimagesize]: failed to open stream: No such file or directory in /htdocs/gall/gal_postphotos.php on line 12Warning: Division by zero in /htdocs/gall/gal_postphotos.php on line 14Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /htdocs/gall/gal_postphotos.php on line 15Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /htdocs/gall/gal_postphotos.php on line 16Warning: imagejpeg(): supplied argument is not a valid Image resource in /htdocs/gall/gal_postphotos.php on line 18Warning: imagecreatefromjpeg(Array) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in /htdocs/gall/gal_postphotos.php on line 22Warning: getimagesize(Array) [function.getimagesize]: failed to open stream: No such file or directory in /htdocs/gall/gal_postphotos.php on line 23Warning: Division by zero in /htdocs/gall/gal_postphotos.php on line 25Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /htdocs/gall/gal_postphotos.php on line 30Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /htdocs/gall/gal_postphotos.php on line 31Warning: imagejpeg(): supplied argument is not a valid Image resource in /htdocs/gall/gal_postphotos.php on line 33Warning: imagedestroy(): supplied argument is not a valid Image resource in /htdocs/gall/gal_postphotos.php on line 36Warning: imagedestroy(): supplied argument is not a valid Image resource in /htdocs/gall/gal_postphotos.php on line 37Warning: imagedestroy(): supplied argument is not a valid Image resource in /htdocs/gall/gal_postphotos.php on line 38Warning: imagedestroy(): supplied argument is not a valid Image resource in /htdocs/gall/gal_postphotos.php on line 39[/quote] Quote Link to comment Share on other sites More sharing options...
Perad Posted November 24, 2006 Share Posted November 24, 2006 [quote]No such file or directory in /htdocs/gall/gal_postphotos.ph[/quote]Check your file paths Quote Link to comment Share on other sites More sharing options...
Adastra Posted November 24, 2006 Author Share Posted November 24, 2006 The file paths are fine, there's no problem with them... Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted November 24, 2006 Share Posted November 24, 2006 what exactly does $_POST['title'] contain. do a print_r($_POST['title']) and let me know.... Quote Link to comment Share on other sites More sharing options...
Adastra Posted November 24, 2006 Author Share Posted November 24, 2006 Right now it's giving me this when I do the print_rArray ( [0] => [1] => ) Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted November 24, 2006 Share Posted November 24, 2006 there is nothing in the array you are using in the foreach function thus producing most of those errors. Quote Link to comment Share on other sites More sharing options...
Adastra Posted November 25, 2006 Author Share Posted November 25, 2006 Ok, then HOW do I do that?? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted November 25, 2006 Share Posted November 25, 2006 is $_POST['title'] receiving anything. is anything being inputted into the title box? Quote Link to comment Share on other sites More sharing options...
Adastra Posted November 25, 2006 Author Share Posted November 25, 2006 Yeah, of course... Anyway, it should also work even if the input box stays blank. Quote Link to comment Share on other sites More sharing options...
akitchin Posted November 25, 2006 Share Posted November 25, 2006 you're mixing up your arrays all over the place. you're naming EVERYTHING as an array, so you need to reference your values by their array as well:[code]$ul_photo = $_FILES['photo'][key]['tmp_name'];$gal = addslashes($_POST['gal_id'][key]);[/code]etc. to get the relevant key, you'll probably want to adjust your foreach() to include it:[code]foreach ($_POST['title'] as $key => $title) {[/code]finally, you may want to enforce a key on each array input, rather than leaving it up to the browser to assign them when submitting:[code]echo "<select name='gal_id[$x]'>";<input type='file' name='photo[<?php echo $x; ?>]' size='50' />[/code]etc. this ensures that all inputs are properly related by their key, in the off-chance that one doesn't register and skews the entire bunch's order. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.