Forum Moderators: coopster

Message Too Old, No Replies

php script giving different number than my form

         

rekhad

5:32 pm on Mar 8, 2006 (gmt 0)

10+ Year Member



I have 2 files in my form


<form enctype="multipart/form-data" action="loadmeup.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="300000">
<input name="userfile[]" type="file" /><br />
<input name="userfile[]" type="file" /><br />
<input type="submit" />
</form>

When i print "$tot",i get 5? also when i print"$result" i get resource id#3
why is that?

<html>
<head><title>Enter Name and Descriptions of Pics</title></head>
<body>
<h1>Enter Name and Descriptions of Pics</h1>
<form action="view.php" method="post">
<?php
//require "db1.inc";
$dbh=mysql_connect("localhost", "root", "password") or die ('I cannot connect to the database.');
mysql_select_db("mydb");

$uploaddir = 'C:\Program Files\EasyPHP1-8\www\images';
if(isset($uploaddir))
print"I exist";
else
print"I do not exist";
$tot = count($_FILES['userfile']);
print"$tot";
$num = 0;
for($q=0;$q<$tot;$q++){

if ($_FILES['userfile']['name'][$q] == "") continue;
$num = $num + 1;
$sql = "SELECT pic_id FROM pics ORDER BY pic_id DESC LIMIT 1";
$result = mysql_query($sql);
print"$result";
while($i = mysql_fetch_array($result)){
$new_id = $i['pic_id'] + 1;
$new_pic = "$new_id.jpg";
if(isset($new_pic))
print"yes";
else
print"i do not exixt";
}
rest of the code...

jatar_k

5:43 pm on Mar 8, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> when i print"$result" i get resource id#3

because all you have at the point you are trying to print is a pointer to a result set in mysql. It is printing the information it has.

take a read through this page
[php.net...]

specifically the "Return Values" portion

rekhad

6:47 pm on Mar 8, 2006 (gmt 0)

10+ Year Member



Thanks for the expalnation,but why count($_FILES['userfile'])is counting 5 instead of 2?

jatar_k

7:01 pm on Mar 8, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



maybe do this to look at what is actually being passed

echo '<pre>';
print_r($_FILES);
echo '</pre>';

it may be passing one for every input element there is but then this

if ($_FILES['userfile']['name'][$q] == "") continue;

makes it skip any that are empty

rekhad

7:22 pm on Mar 8, 2006 (gmt 0)

10+ Year Member



the output is

Array
(
[userfile] => Array
(
[name] => Array
(
[0] => Bulls.jpg
[1] => Henry.jpg
)

[type] => Array
(
[0] => image/pjpeg
[1] => image/pjpeg
)

[tmp_name] => Array
(
[0] => C:/Program Files/EasyPHP1-8\tmp\php31.tmp
[1] => C:/Program Files/EasyPHP1-8\tmp\php32.tmp
)

[error] => Array
(
[0] => 0
[1] => 0
)

[size] => Array
(
[0] => 21501
[1] => 9938
)

)

)

so only 2 files are being passed.

jatar_k

7:28 pm on Mar 8, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



aha, but you are looking at it in light of what you want, not what it is actually doing

count($_FILES['userfile'])

should return 5, there are five elements in that level of the array

but

count($_FILES['userfile']['name'])

should return what you want, just the number of files passed

rekhad

9:46 pm on Mar 8, 2006 (gmt 0)

10+ Year Member



I need your help one more time.I am trying my best to understand the code..
But getting error here

My line 24 is
if ($_FILES['userfile']['name'][$q] == "") continue;

My line 36 is

if (move_uploaded_file($_FILES['userfile']['tmp_name'][$q], $uploaddir.$new_pic)) {

Henry.jpg
Notice: Undefined variable: new_pic in c:\program files\easyphp1-8\www\loadmeup.php on line 36

Warning: move_uploaded_file(C:\Program Files\EasyPHP1-8\www\images): failed to open stream: Permission denied in c:\program files\easyphp1-8\www\loadmeup.php on line 36

Warning: move_uploaded_file(): Unable to move 'C:/Program Files/EasyPHP1-8\tmp\php47.tmp' to 'C:\Program Files\EasyPHP1-8\www\images' in c:\program files\easyphp1-8\www\loadmeup.php on line 36
(Array{['userfile']}{['name']}[0]) did not upload! //
// // // Henry.jpg

Name:

Description:

Bulls.jpg
Notice: Undefined variable: new_pic in c:\program files\easyphp1-8\www\loadmeup.php on line 36

Warning: move_uploaded_file(C:\Program Files\EasyPHP1-8\www\images): failed to open stream: Permission denied in c:\program files\easyphp1-8\www\loadmeup.php on line 36

Warning: move_uploaded_file(): Unable to move 'C:/Program Files/EasyPHP1-8\tmp\php48.tmp' to 'C:\Program Files\EasyPHP1-8\www\images' in c:\program files\easyphp1-8\www\loadmeup.php on line 36
(Array{['userfile']}{['name']}[1]) did not upload! //
// // // Bulls.jpg

Name:

Description:

Notice: Undefined offset: 2 in c:\program files\easyphp1-8\www\loadmeup.php on line 24

Notice: Undefined offset: 3 in c:\program files\easyphp1-8\www\loadmeup.php on line 24

Notice: Undefined offset: 4 in c:\program files\easyphp1-8\www\loadmeup.php on line 24

<html>
<head><title>Enter Name and Descriptions of Pics</title></head>
<body>
<h1>Enter Name and Descriptions of Pics</h1>
<form action="view.php" method="post">
<?php
//require "db1.inc";
$dbh=mysql_connect("localhost", "root", "password") or die ('I cannot connect to the database.');
mysql_select_db("mydb");

$uploaddir = 'C:\Program Files\EasyPHP1-8\www\images';

$tot = count($_FILES['userfile']);

for($q=0;$q<$tot;$q++){

if ($_FILES['userfile']['name'][$q] == "") continue;
print_r(($_FILES['userfile']['name'][$q]));
$num = $num + 1;
$sql = "SELECT pic_id FROM pics ORDER BY pic_id DESC LIMIT 1";
$result = mysql_query($sql);

while($i = mysql_fetch_array($result)){
$new_id = $i['pic_id'] + 1;
print"$new_id";
$new_pic = "$new_id.jpg";

}
if (move_uploaded_file($_FILES['userfile']['tmp_name'][$q], $uploaddir.$new_pic)) {
$sql = "INSERT INTO pics ( pic_id , pic_name , descrip )VALUES ('$new_id', NULL , NULL);";
mysql_query($sql);
} else {
print "<strong>($_FILES{['userfile']}{['name']}[$q])</strong> did not upload!";
}

?>
//<div style="clear: both;">
//<a href="/images/<?=$new_pic?>">
//<img src="/images/<?=$new_thumb?>" style="float: left" />
//</a>
<strong><?=$_FILES['userfile']['name'][$q]?></strong><br /><br />
<strong>Name:</strong><br />
<input type="text" name="pic_name[<?=$num?>]" /><br /><br />
<strong>Description:</strong><br />
<textarea name="descrip[<?=$num?>]"></textarea>
<input type="hidden" name="pic_id[<?=$num?>]" value="<?=$new_id?>" />
//</div>
<?php
}
?>
<input type="submit" value="Click to Save Descriptions" />
</form>
</body>
</html>

whoisgregg

10:07 pm on Mar 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$new_pic = "$new_id.jpg";

Should be:

$new_pic = $new_id.'.jpg';

You can also use the curly braces {} like so:

$new_pic = "{$new_id}.jpg";

More details in the strings section [us2.php.net] of the manual.

rekhad

11:35 pm on Mar 8, 2006 (gmt 0)

10+ Year Member




$new_id=$new_pic.'.jpg'; //doesn't work
$new_id={$new_pic}.jpg; //doesn't work

rekhad

11:37 pm on Mar 8, 2006 (gmt 0)

10+ Year Member



sorry it other way

$new_pic={$new_id}.jpg;//doesn't work
$new_pic=$new_id.'.jpg';//doesn't work

jatar_k

12:58 am on Mar 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what if you change this

if (move_uploaded_file($_FILES['userfile']['tmp_name'][$q], $uploaddir.$new_pic)) {

to
$mypic = $uploaddir.$new_pic;
echo $mypic;
if (move_uploaded_file($_FILES['userfile']['tmp_name'][$q], $mypic)) {

just to see if it is correct

I still think it is a path issue

Warning: move_uploaded_file(): Unable to move 'C:/Program Files/EasyPHP1-8\tmp\php47.tmp' to 'C:\Program Files\EasyPHP1-8\www\images' in c:\program files\easyphp1-8\www\loadmeup.php on line 36

the slashes flip flop and the upper and lower case mismatch seems weird in this

Warning: move_uploaded_file(): Unable to move 'C:/Program Files/EasyPHP1-8\tmp\php47.tmp' to 'C:\Program Files\EasyPHP1-8\www\images' in c:\program files\easyphp1-8\www\loadmeup.php on line 36

rekhad

1:09 am on Mar 9, 2006 (gmt 0)

10+ Year Member



The output is still the same if I change

if (move_uploaded_file($_FILES['userfile']['tmp_name'][$q], $uploaddir.$new_pic)) {

to

$mypic=$uploaddir.$new_pic
echo $mypic;
if (move_uploaded_file($_FILES['userfile']['tmp_name'][$q], $mypic)) {

It is not echoing $mypic?

jatar_k

1:21 am on Mar 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



nothing at all? it should at least be spitting out the directory if not the filename

rekhad

2:37 am on Mar 9, 2006 (gmt 0)

10+ Year Member



are there some other simple resources to upload images in database and read them..