Jump to content

[SOLVED] Multi-image upload problems


Grimloch

Recommended Posts

Lets see if I can get some help on this one. Can anyone show me what I am doing wrong here. I'm just trying at this point to verify that the form inputs will work right. $max_img is set to 6. Here is the code:

 

<form enctype='multipart/form-data' name='inputform' method='post' action='".FUSION_SELF."?op=Add'>

for($i=1; $i<=$max_img; $i++){
echo "<tr><td>".$locale['CLS_0103']."$i:</td><td>
<input class='textbox' type='file' name='image[]' size='45' maxlength='150' value='http://'></td></tr>";
}

</form>

///////////////////////

//image upload handling

$array = (isset($_POST['image']));
$list = implode(",",$array);
$ad_pic = $list;
$list_array = explode(",",$ad_pic);
reset($list_array);
foreach($list_array AS $picture) {

echo $picture."<br />";

}

// end upload handling

die;

 

After 'die' executes I get no output. Any help appreciated.

Link to comment
Share on other sites

The problem is: I get nothing echoed to the screen so my question is am I going about this the right way and is my input routine correct? If I can get the input routine to work right then I can convert the array elements to strings for actual uploading and saving to the upload directory. And THEN I can figure out the db update process.

Link to comment
Share on other sites

Not really solved but I've come to expect little help here lately for some reason.

keep in mind, you are not paying for the advice/examples/help given on this site, so expect nothing, appreciate everything that is given to you.

 

people offer their help in their free time, and if they choose not to spend their free time helping you, that's their prerogative.

 

try this:

 

<?php
while (list ($key, $value) = each($_FILES['image']['name']))
{
if (!empty ($value))
{ echo $_FILES['image']['name'][$key]; }
}
?>

 

something like that.

Link to comment
Share on other sites

Good morning mrMarkus (or anyone that may have some time to look at this);

 

OK... tried your code and it gives me the output I was looking for.

Let me lay the whole thing out and see if I can explain what I am trying to accomplish here.

1) These are the dbFields where the images and thumbs will be stored:

 

img1 varchar(100) NOT NULL default '',
img2 varchar(100) NOT NULL default '',
img3 varchar(100) NOT NULL default '',
img4 varchar(100) NOT NULL default '',
img5 varchar(100) NOT NULL default '',
img6 varchar(100) NOT NULL default '',
thb1 varchar(100) NOT NULL default '',
thb2 varchar(100) NOT NULL default '',
thb3 varchar(100) NOT NULL default '',
thb4 varchar(100) NOT NULL default '',
thb5 varchar(100) NOT NULL default '',
thb6 varchar(100) NOT NULL default '',

 

2) This is your code mrMarkus w/my changes:

 

// ad image upload
while (list ($key, $value) = each($_FILES['image']['tmp_name']))
{
if (!empty ($value))
{ echo $_FILES['image']['name'][$key]."_";echo $_FILES['image']['tmp_name'][$key]."<br /><br />"; }
}

// end ad image upload

 

3) These are the images of the input/output produced:

OOooppss... img input names are from a different test session; but you get the idea...

 

Input

Image_Input_Test.jpg

 

Output

Image_Output_Test.jpg

 

4) This is how I need to store the images in db:

 

$img1 = downloads.gif
$img2 = hardware.gif
$img3 = members.gif
$img4 = network.gif
$img5 = software.gif
$img6 = graphics.gif
----------
$thb1 = downloads_t1.gif
$thb2 = hardware_t1.gif
$thb3 = members_t1.gif
$thb4 = network_t1.gif
$thb5 = software_t1.gif
$thb6 = graphics_t1.gif

 

Of course these are just test images; actual uploads will almost always be JPGs.

I have no idea how to go about saving those uploads into the specified directory; I'm assuming it ALL has to be done within the (while loop) is that right? As well as the thumb creation and save??

 

Earlier I had tried a slight variation of the very code that you showed me mrMarkus but I got errors saying that I was trying to save array elements instead of strings. So the array elements which contain the image names must first be converted to strings I think. There again I don't know how to go about that.

 

I know this is long-winded and I apologize. If anyone has the time to take a look at this and help me I will be most appreciative.

Link to comment
Share on other sites

I'm trying to get this down to just basics first. Below is the code I am trying to use now to upload 6 images(or less) to my 'save dir'. It does not work; nothing is saved in the upload dir. What am I doing wrong here??

 

The input portion:

 

for($i=1; $i<=$max_img; $i++){
echo "<tr><td>".$locale['CLS_0103']."$i:</td><td>
<input id='adimg' class='textbox' type='file' name='adimg[]' size='45' maxlength='150'></td></tr>";}

 

The upload/save portion:

 

// ad image upload
    if(isset($_POST[adimg])) {

while (list ($key, $value) = each($_FILES['adimg']['tmp_name']))
{
if (!empty ($value))
	$imagename = $_FILES['adimg']['name'];
	$source = $_FILES['adimg']['tmp_name'];
	$target = INFUSIONS.'classified_ads_panel/images/ads/'.$imagename;
	move_uploaded_file($source, $target);
}
}
// end ad image upload

Link to comment
Share on other sites

Is $_POST[adimg] even set?

 

The first step in troubleshooting any piece of code is to determine if it is receiving the expected data. The form code you have posted anywhere in this thread does not have a field that would set $_POST[adimg].

 

To see exactly what your code is receiving, use the following -

 

echo "<pre>";
echo "POST:";
print_r($_POST);
echo "FILES:";
print_r($_FILES);
echo "</pre>";

 

Edit: And I have to ask, are you developing and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON in your master php.ini to get php to help you by displaying all the errors that it detects? You will save a ton of time. Stop and start your web server to get any changes made to php.ini to take effect and confirm that the settings are actually changed using a phpinfo() statement in case the php.ini that you are changing is not the one that php is using.

Link to comment
Share on other sites

You were right. I should've been checking for (submit). Thanks!

This is what I have in place now:

 

// ad image upload
    if(isset($_POST[submit])) {

while (list ($key, $value) = each($_FILES['adimg']['tmp_name']))
{
if (!empty ($value))
{ echo $_FILES['adimg']['name'][$key]." | ".$_FILES['adimg']['tmp_name'][$key]."<br /><br />"; }
echo "<pre>";
echo "POST:";
print_r($_POST);
echo "FILES:";
print_r($_FILES);
echo "</pre>";
}
//		$imagename = $_FILES['adimg']['name'];
//		$source = $_FILES['adimg']['tmp_name'];
//		$target = INFUSIONS.'classified_ads_panel/images/ads/'.$imagename;
//		move_uploaded_file($source, $target);
//	}

}
// end ad image upload
die;

 

And the above produces the following output:

 

windows.gif | /tmp/phpMnDkur


POST:Array
(
    [cat] => 8
    [title] => Testing
    [description] => Testing again!
    [bbcolor] => 
    [url] => http://somewhere.net
    [weeks] => 7
    [owner] => Gargon
    [owner_id] => 1
    [op] => Add
    [submit] => Submit
)
FILES:Array
(
    [adimg] => Array
        (
            [name] => Array
                (
                    [0] => windows.gif
                    [1] => themes.gif
                    [2] => software.gif
                    [3] => security.gif
                    [4] => php-fusion.gif
                    [5] => network.gif
                )

            [type] => Array
                (
                    [0] => image/gif
                    [1] => image/gif
                    [2] => image/gif
                    [3] => image/gif
                    [4] => image/gif
                    [5] => image/gif
                )

            [tmp_name] => Array
                (
                    [0] => /tmp/phpMnDkur
                    [1] => /tmp/phpeM6MNI
                    [2] => /tmp/phpwLtne0
                    [3] => /tmp/phpw51ZCh
                    [4] => /tmp/phpSGxXJB
                    [5] => /tmp/phpC8tUOV
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                    [4] => 0
                    [5] => 0
                )

            [size] => Array
                (
                    [0] => 5160
                    [1] => 5075
                    [2] => 5607
                    [3] => 5454
                    [4] => 5494
                    [5] => 5503
                )

        )

)
themes.gif | /tmp/phpeM6MNI


POST:Array
(
    [cat] => 8
    [title] => Testing
    [description] => Testing again!
    [bbcolor] => 
    [url] => http://somewhere.net
    [weeks] => 7
    [owner] => Gargon
    [owner_id] => 1
    [op] => Add
    [submit] => Submit
)
FILES:Array
(
    [adimg] => Array
        (
            [name] => Array
                (
                    [0] => windows.gif
                    [1] => themes.gif
                    [2] => software.gif
                    [3] => security.gif
                    [4] => php-fusion.gif
                    [5] => network.gif
                )

            [type] => Array
                (
                    [0] => image/gif
                    [1] => image/gif
                    [2] => image/gif
                    [3] => image/gif
                    [4] => image/gif
                    [5] => image/gif
                )

            [tmp_name] => Array
                (
                    [0] => /tmp/phpMnDkur
                    [1] => /tmp/phpeM6MNI
                    [2] => /tmp/phpwLtne0
                    [3] => /tmp/phpw51ZCh
                    [4] => /tmp/phpSGxXJB
                    [5] => /tmp/phpC8tUOV
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                    [4] => 0
                    [5] => 0
                )

            [size] => Array
                (
                    [0] => 5160
                    [1] => 5075
                    [2] => 5607
                    [3] => 5454
                    [4] => 5494
                    [5] => 5503
                )

        )

)
software.gif | /tmp/phpwLtne0


POST:Array
(
    [cat] => 8
    [title] => Testing
    [description] => Testing again!
    [bbcolor] => 
    [url] => http://somewhere.net
    [weeks] => 7
    [owner] => Gargon
    [owner_id] => 1
    [op] => Add
    [submit] => Submit
)
FILES:Array
(
    [adimg] => Array
        (
            [name] => Array
                (
                    [0] => windows.gif
                    [1] => themes.gif
                    [2] => software.gif
                    [3] => security.gif
                    [4] => php-fusion.gif
                    [5] => network.gif
                )

            [type] => Array
                (
                    [0] => image/gif
                    [1] => image/gif
                    [2] => image/gif
                    [3] => image/gif
                    [4] => image/gif
                    [5] => image/gif
                )

            [tmp_name] => Array
                (
                    [0] => /tmp/phpMnDkur
                    [1] => /tmp/phpeM6MNI
                    [2] => /tmp/phpwLtne0
                    [3] => /tmp/phpw51ZCh
                    [4] => /tmp/phpSGxXJB
                    [5] => /tmp/phpC8tUOV
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                    [4] => 0
                    [5] => 0
                )

            [size] => Array
                (
                    [0] => 5160
                    [1] => 5075
                    [2] => 5607
                    [3] => 5454
                    [4] => 5494
                    [5] => 5503
                )

        )

)
security.gif | /tmp/phpw51ZCh


POST:Array
(
    [cat] => 8
    [title] => Testing
    [description] => Testing again!
    [bbcolor] => 
    [url] => http://somewhere.net
    [weeks] => 7
    [owner] => Gargon
    [owner_id] => 1
    [op] => Add
    [submit] => Submit
)
FILES:Array
(
    [adimg] => Array
        (
            [name] => Array
                (
                    [0] => windows.gif
                    [1] => themes.gif
                    [2] => software.gif
                    [3] => security.gif
                    [4] => php-fusion.gif
                    [5] => network.gif
                )

            [type] => Array
                (
                    [0] => image/gif
                    [1] => image/gif
                    [2] => image/gif
                    [3] => image/gif
                    [4] => image/gif
                    [5] => image/gif
                )

            [tmp_name] => Array
                (
                    [0] => /tmp/phpMnDkur
                    [1] => /tmp/phpeM6MNI
                    [2] => /tmp/phpwLtne0
                    [3] => /tmp/phpw51ZCh
                    [4] => /tmp/phpSGxXJB
                    [5] => /tmp/phpC8tUOV
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                    [4] => 0
                    [5] => 0
                )

            [size] => Array
                (
                    [0] => 5160
                    [1] => 5075
                    [2] => 5607
                    [3] => 5454
                    [4] => 5494
                    [5] => 5503
                )

        )

)
php-fusion.gif | /tmp/phpSGxXJB


POST:Array
(
    [cat] => 8
    [title] => Testing
    [description] => Testing again!
    [bbcolor] => 
    [url] => http://somewhere.net
    [weeks] => 7
    [owner] => Gargon
    [owner_id] => 1
    [op] => Add
    [submit] => Submit
)
FILES:Array
(
    [adimg] => Array
        (
            [name] => Array
                (
                    [0] => windows.gif
                    [1] => themes.gif
                    [2] => software.gif
                    [3] => security.gif
                    [4] => php-fusion.gif
                    [5] => network.gif
                )

            [type] => Array
                (
                    [0] => image/gif
                    [1] => image/gif
                    [2] => image/gif
                    [3] => image/gif
                    [4] => image/gif
                    [5] => image/gif
                )

            [tmp_name] => Array
                (
                    [0] => /tmp/phpMnDkur
                    [1] => /tmp/phpeM6MNI
                    [2] => /tmp/phpwLtne0
                    [3] => /tmp/phpw51ZCh
                    [4] => /tmp/phpSGxXJB
                    [5] => /tmp/phpC8tUOV
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                    [4] => 0
                    [5] => 0
                )

            [size] => Array
                (
                    [0] => 5160
                    [1] => 5075
                    [2] => 5607
                    [3] => 5454
                    [4] => 5494
                    [5] => 5503
                )

        )

)
network.gif | /tmp/phpC8tUOV


POST:Array
(
    [cat] => 8
    [title] => Testing
    [description] => Testing again!
    [bbcolor] => 
    [url] => http://somewhere.net
    [weeks] => 7
    [owner] => Gargon
    [owner_id] => 1
    [op] => Add
    [submit] => Submit
)
FILES:Array
(
    [adimg] => Array
        (
            [name] => Array
                (
                    [0] => windows.gif
                    [1] => themes.gif
                    [2] => software.gif
                    [3] => security.gif
                    [4] => php-fusion.gif
                    [5] => network.gif
                )

            [type] => Array
                (
                    [0] => image/gif
                    [1] => image/gif
                    [2] => image/gif
                    [3] => image/gif
                    [4] => image/gif
                    [5] => image/gif
                )

            [tmp_name] => Array
                (
                    [0] => /tmp/phpMnDkur
                    [1] => /tmp/phpeM6MNI
                    [2] => /tmp/phpwLtne0
                    [3] => /tmp/phpw51ZCh
                    [4] => /tmp/phpSGxXJB
                    [5] => /tmp/phpC8tUOV
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                    [4] => 0
                    [5] => 0
                )

            [size] => Array
                (
                    [0] => 5160
                    [1] => 5075
                    [2] => 5607
                    [3] => 5454
                    [4] => 5494
                    [5] => 5503
                )

        )

) 

 

I tried uncommenting the upload/save portion and it hangs the server; so my question is still; how do I code it correctly to actually upload and save the images??

Link to comment
Share on other sites

you need to assign a $key to your $_FILES['adimg']['name'], $_FILES['adimg']['tmp_name'], etc:

 

<?php
$imagename = $_FILES['adimg']['name'][$key]; //added [$key]
$source = $_FILES['adimg']['tmp_name'][$key]; //added [$key]
$target = INFUSIONS.'classified_ads_panel/images/ads/'.$imagename;
move_uploaded_file($source, $target);
?>

 

try that.

Link to comment
Share on other sites

OK; I don't quite understand whats happening now... I implemented the code changes and WALLA! the 6 files were uploaded! Seemed great. But then I deleted the files on the server to try again(was trying to implement saving to db; that didn't work); so I deleted that save to db code and now, it WILL NOT upload images again with subsequent test posts. Doesn't make sense to me.  I figured maybe cookies/sessions/variables not cleared so I logged on and off my site several times. That has not helped. I don't understand. Here is the code now:

 

// ad image upload
    if(isset($_POST[submit])) {

while (list ($key, $value) = each($_FILES['adimg']['tmp_name']))
{
if (!empty ($value))
	$imagename = $_FILES['adimg']['name'][$key];
	$source = $_FILES['adimg']['tmp_name'][$key];
	$target = INFUSIONS.'classified_ads_panel/images/ads/'.$imagename;
	move_uploaded_file($source, $target);
}
}
// end ad image upload

Link to comment
Share on other sites

Its not really that long and I thought it might be beneficial to anyone trying to help me out, to post the entire code (just the portions that deal with adding a new ad). Here it is:

 

function add_ad() {
global $userdata, $mostweeks, $defaultweeks, $adstext, $locale, $max_img, $db_prefix;
echo "
<table border='0' cellspacing='2' cellpadding='3' width='100%'>
<tr><td>";
	$mainlink = 1;
	menu($mainlink);
echo "
</td></tr>
<tr><td colspan='2'></td></tr>
<tr><td class='small' colspan='2'><br />";
	SearchForm();
echo "
</td></tr>
<tr><td colspan='2'></td></tr>
<tr><td colspan='2'><b>".$locale['CLS_0031']."</b></td></tr>
<tr><td colspan='2'></td></tr>";
if (iMEMBER)
{
	echo "
	<tr><td>".$locale['CLS_0005']." <b>".$userdata['user_name']."</b>, ".$locale['CLS_0032']."</td></tr>
	<tr><td></td></tr>
	<tr><td>
		<form enctype='multipart/form-data' name='inputform' method='post' action='".FUSION_SELF."?op=Add'>
    			<table>
		<tr><td>".$locale['CLS_0033']."</td>
		<td><SELECT class='textbox' name='cat'>";
			echo "<option value=''>".$locale['CLS_0034']."</option>\n";
		$result = dbquery("SELECT cid, title, parentid FROM ".DB_CLASSIFIED_CATEGORIES." WHERE status='1' ORDER BY parentid,title");
		while(list($cid2, $ctitle2, $parentid2) = dbarraynum($result)) 
		{
			if ($parentid2!=0) $ctitle2=getparent($parentid2,$ctitle2);
			echo "<option value='$cid2'>$ctitle2</option>\n";
		}		
	echo "</SELECT>
	</td></tr>
	<tr><td>".$locale['CLS_0035']."</td><td><input class='textbox' type='text' name='title' size='45' maxlength='100' /></td></tr>
   		<tr><td valign='top'>".$locale['CLS_0036']."</td>
	       <td><textarea class='textbox' name='description' cols='40' rows='15' wrap='virtual'></textarea></td></tr></tr>
		<tr><td> </td><td>";
		class_showbbcode('description');
		echo "</td></tr>
		<tr><td> </td><td>";
		class_showbbcodecolor('description');
		echo "</td></tr>
    		<tr><td> </td><td>".$locale['CLS_0037']."$adstext characters.</td></tr>
    		<tr><td>".$locale['CLS_0025']."</td><td><input class='textbox' type='text' name='url' size='45' maxlength='100' value='http://' /></td></tr>";
		for($i=1; $i<=$max_img; $i++){
		echo "<tr><td>".$locale['CLS_0103']."$i:</td><td>
		<input id='adimg' class='textbox' type='file' name='adimg[]' size='45' maxlength='150'></td></tr>";
		}
    		echo "<tr><td>".$locale['CLS_0030'].":</td><td>";
		prweek($mostweeks, $defaultweeks);
	echo " ".$locale['CLS_0040']."</td></tr>
	<tr><td colspan='2' align='center'>
		<input type='hidden' name='owner' value='".$userdata['user_name']."' />
		<input type='hidden' name='owner_id' value='".$userdata['user_id']."' />
    			<input type='hidden' name='op' value='Add' />
		<input class='button' type='submit' name='submit' value='".$locale['CLS_0038']."' />
	</td></tr>
	</table>
	</form>
	</td></tr>";
} else {
	echo "
	<tr>
		<td>
		<p>".$locale['CLS_0039']."</td>
	</tr>";
}
echo "
</table>";
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
function Add($cat, $title, $description, $url, $weeks, $owner, $owner_id)
{
global $adstext, $locale, $userdata, $settings, $db_prefix;
//require_once INCLUDES."photo_functions_include.php";
$postdate = time();
$expiredate = $postdate + (3600 * 24 * $weeks);
$url = explode("http://", $url);
if ($url[1]=="")
{
	$myurl = "";
} else {
	$myurl ="$url[1]";
}
// Check if Category exist
if ($cat=="")
{
	echo "<br />";
	$mainlink = 1;
	menu($mainlink);
	echo "<br /><br />".$locale['CLS_0041']."<br />";
	exit();
}
// Check if Title exist
if ($title=="")
{
	echo "<br />";
	$mainlink = 1;
	menu($mainlink);
	echo "<br /><br />".$locale['CLS_0042']."<br />";
	exit();
}
// Check if ads text exist
if ($description=="")
{
	echo "<br />";
	$mainlink = 1;
	menu($mainlink);
	echo "<br /><br />".$locale['CLS_0043']."<br />";
	exit();
} 
// Check ads text length	
if (strlen($description) > $adstext) 
{
	echo "<br />";
	$mainlink = 1;
	menu($mainlink);
	echo "<br /><br />".$locale['CLS_0044'].": $adstext characters.<br />";
	exit();
} 
$title = addslashes($title);
$description = addslashes($description);
// ad image upload
    if(isset($_POST[submit])) {

while (list ($key, $value) = each($_FILES['adimg']['tmp_name']))
{
if (!empty ($value))
	$imagename = $_FILES['adimg']['name'][$key];
	$source = $_FILES['adimg']['tmp_name'][$key];
	$target = INFUSIONS.'classified_ads_panel/images/ads/'.$imagename;
	move_uploaded_file($source, $target);
}
}
// end ad image upload

$result = dbquery("INSERT INTO ".DB_CLASSIFIED." VALUES(NULL, '$cat', '$title', '$description', '$myurl', '$postdate', '$expiredate', '$owner', '$owner_id','0','$img1','$img2','$img3','$img4','$img5','$img6','$thb1','$thb2','$thb3','$thb4','$thb5','$thb6')");
$result = dbquery("delete low_priority FROM ".DB_CLASSIFIED." WHERE(expiredate < '$postdate')");
echo $locale['CLS_0108'];
redirect(FUSION_SELF."?op=view_ad&cid=$cat",'2');
}

 

All main script areas(functions) are called from a 'switch' setup at the very end of the main script just to clarify.

Link to comment
Share on other sites

OK; I don't quite understand whats happening now... I implemented the code changes and WALLA! the 6 files were uploaded! Seemed great. But then I deleted the files on the server to try again(was trying to implement saving to db; that didn't work); so I deleted that save to db code and now, it WILL NOT upload images again with subsequent test posts. Doesn't make sense to me.  I figured maybe cookies/sessions/variables not cleared so I logged on and off my site several times. That has not helped. I don't understand. Here is the code now:

 

OK this is wrong; the problem was I was uploading images from the same few. I guess I just lost track. The code DOES in fact upload the images consistently now. I just have to figure out how to do proper image type/size checking and generate thumbs. Then I have to figure out how to extract the image names and thumb names for updating the db. And I'm pretty sure this ALL has to be done within the 'while loop' because I tried echoing the image names after the loop/upload and it doesn't work.

Link to comment
Share on other sites

I need to close this thread as solved because the actual problem (multi-image upload), is now working although this is only 1/3 of the entire equation I need to solve. I will either start a new thread or seek other resources for help.

 

Thanks to all who have helped me here.

 

:D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.