Uploading multiple files then resize

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
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Uploading multiple files then resize

Post by psychotomus »

Everah | Changed [ code ] tags to [ php ] tags.

Warning: imagecreatefromjpeg(screens/KnowWiiDOTcom) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in /mounted-storage/home29a/sub002/sc18478-RGIJ/knowwii.com/add_screen_shot.php

My first file is called file[0] then my 2nd file is called file[1]

any i deas on how to fix this problem?

Code: Select all

$files = array_values($HTTP_POST_VARS['file']);

$user_id = $userdata['user_id'];
$game_id = $HTTP_GET_VARS['id'];

$date = time();

//loop threw all cheats submitted
for ($x =0; $x < count($HTTP_POST_VARS['file']); $x++)
{
	//if cheat title or cheat not blank
	if ( $files[$x] <> "")
	{
		print $_FILES["files[$x]"]["name"] .'<br>';
		$pos = strpos($_FILES["files[$x]"]["name"],'.');
		$file_type =  strtolower(substr($_FILES["files[$x]"]["name"], $pos + 1, strlen($_FILES["files[$x]"]["name"]) - $pos +1));
		move_uploaded_file($_FILES["files[$x]"]["tmp_name"], "screens/KnowWiiDOTcom" . $_FILES["files[$x]"]["name"]);
		
		// Create thumbnail canvas 
		$destimg = imagecreatetruecolor ("320", "240") or die ("Problem In Creating image"); 

		// Use one or use conditional to select i.e. if JPEG 
		$srcimg = imagecreatefromjpeg ("screens/KnowWiiDOTcom" . $_FILES["files[$x]"]["name"]) or die ("Problem In opening Source Image");  // For JPEG 
		
		// Resample the image from $srcimg to the $destimg
		imagecopyresampled ($destimg, $srcimg, 0, 0, 0, 0, "320", "240", imagesx($srcimg), imagesy($srcimg)) or die ("Problem In resizing");   
		
		// Save JPG 
		imagejpeg ($destimg, "screens/KnowWiiDOTcom_thumb_" . $_FILES["files[$x]"]["name"]) or die("Problem In saving");  
}
Everah | Changed [ code ] tags to [ php ] tags.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Post your html form code. Looks like $_FILES["files[$x]"]["name"] is null.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can you do a var_dump() of $_POST and $FILES?

Code: Select all

<?php
echo '<pre>';
var_dump($_POST);
echo '</pre>';

echo '<pre>';
var_dump($_FILES);
echo '</pre>';
?>
See what that tells you.
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

dont understand this to well.

Code: Select all

array(3) {
  ["file"]=>
  array(2) {
    [0]=>
    string(121) "C:\\Documents and Settings\\psychotomus\\Desktop\\know wii\\Call of Duty 3\\screens\\call-of-duty-3-20061006114013302.jpg"
    [1]=>
    string(121) "C:\\Documents and Settings\\psychotomus\\Desktop\\know wii\\Call of Duty 3\\screens\\call-of-duty-3-20060929052357006.jpg"
  }
  ["hiddenField"]=>
  string(1) "2"
  ["Submit2"]=>
  string(18) "Add Screen Shot(s)"
}

array(0) {
}


array(3) {
  ["file"]=>
  array(2) {
    [0]=>
    string(121) "C:\\Documents and Settings\\psychotomus\\Desktop\\know wii\\Call of Duty 3\\screens\\call-of-duty-3-20061006114013302.jpg"
    [1]=>
    string(121) "C:\\Documents and Settings\\psychotomus\\Desktop\\know wii\\Call of Duty 3\\screens\\call-of-duty-3-20060929052357006.jpg"
  }
  ["hiddenField"]=>
  string(1) "2"
  ["Submit2"]=>
  string(18) "Add Screen Shot(s)"
}

array(0) {
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Is there a reason you're mixing old (long style) and new style superglobals?

I'd like to see the form that's being used to submit to this code.
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

Code: Select all

<form name="form1" method="post" action="">
  <table width="100%"  border="1">
    <tr>
      <td><div align="right"><strong>Screen Shot #0:</strong></div></td>
      <td><input type="file" name="file[0]" id="file[0]" /> </td>
    </tr>
    <tr>
      <td><div align="right"><strong>Screen Shot #1:</strong></div></td>
      <td><input type="file" name="file[1]" id="file[1]" /> </td>
    </tr>
    <tr>
      <td><div align="right"><strong>Screen Shot #2:</strong></div></td>
      <td><input type="file" name="file[2]" id="file[2]" /> </td>
    </tr>
  </table>
  <div align="center">
    <input type="hidden" name="hiddenField" value="3">
    <input name="Submit2" type="submit" id="Submit2" value="Add Screen Shot(s)">
  </div>
</form>
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Try using $_FILES['file'][$x]['name'] at the line that the error points to
Last edited by aaronhall on Thu Dec 07, 2006 9:45 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$_FILES['file'][0]
Your form doesn't have the enctype attribute set.
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

Heres what I got I get no errors, but it doens't upload the files. folder is also set to 777 and i fixed the html.

the print $_FILES["file"]["name"][$x] . " $x <br>"; actually provides the correct name so all my naming should be correct.

Code: Select all

$file_count = $HTTP_POST_VARS['hiddenField'];

$user_id = $userdata['user_id'];
$game_id = $HTTP_GET_VARS['id'];


//loop threw all screen shots submitted
for ($x =0; $x < $file_count; $x++)
{
	print $_FILES["file"]["name"][$x] . " $x <br>";

	//if file field not blank
	if ( $_FILES["file"]["name"][$x]<> "")
	{
		$pos = strpos($_FILES["file"]["name"][$x],'.');
		$file_type =  strtolower(substr($_FILES["file"]["name"][$x], $pos + 1, strlen($_FILES["file"]["name"][$x]) - $pos +1));
		move_uploaded_file($_FILES["file"]["tmp_name"][$x], "screens/KnowWiiDOTcom" . $_FILES["file"]["name"][$x]);
		
		// Create thumbnail canvas 
		$destimg = imagecreatetruecolor ("320", "240") or die ("Problem In Creating image"); 

		// Use one or use conditional to select i.e. if JPEG 
		$srcimg = imagecreatefromjpeg ("screens/KnowWiiDOTcom" . $_FILES["file"]["name"][$x]) or die ("Problem In opening Source Image");  // For JPEG 
		
		// Resample the image from $srcimg to the $destimg
		imagecopyresampled ($destimg, $srcimg, 0, 0, 0, 0, "320", "240", imagesx($srcimg), imagesy($srcimg)) or die ("Problem In resizing");   
		
		// Save JPG 
		imagejpeg ($destimg, "screens/KnowWiiDOTcom_thumb_" . $_FILES["file"]["name"][$x]) or die("Problem In saving");  
		
	}
}
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

You replaced the $_FILES references incorrectly. Double check it and post the code again if it doesn't work.
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

nevermind. it works. can't believe i spent like 4 hours looking at php when it was an html problem. thanks feyd. you saved me many more hours =)
Post Reply