Jump to content

Recommended Posts

Ok ... so here is a snippet hopefully you can see wot i am doin from this. in the $arr variable there are files that have been sent to the server through a file <input>

foreach($arr as $var){
unset($imagename);

if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;

if(!isset($_FILES["$var"]))
$error["$var"] = "An image was not found.";

$filename = basename($_FILES["$var"]['name']);
$maxsize = 2621440;
if(filesize($filename) > $maxsize)
$error["$var"] = "The image is too large, the image must be ". $maxsize ." bytes or smaller" ;
echo $error["$var"];

I am kinda new to php but the code I submitted is just a small snippet to show what i am trying to do with the code. Just imagine it as a completely working page. everything is working i just need to get a variable into the $_FILES["  :( ]

 

I need to get the $var into the $_FILES["    "] so that the checking of the files and the making of thumbnails work.

Why not just foreach the files. If $_FILES is a multi-dimensional array this would be better.

 

<?php
foreach ($_FILES as $key => $val) {
    if (is_array($val)) {
          echo "File at $key is named " . $_FILES[$key]['name'] . "<br />"; 
    }
}
?>

 

I am not keen on file uploads, but see what that gives you. If it works, then just modify items inside the if statement to do what you want. If not maybe look for a multiple file upload php tutorial and see how it is really done.

ok ... i dont understand how that would help me ... this is the code i am working with:

 

<?php

/**
* @author JAG
* @copyright 2009
*/
include "data/header.php";
include "data/dbconnect.php";

if($_POST["Submit"])
{
if(!empty($_FILES["image_file1"])){
	echo "theres summit in here!!!!!";
}

echo $_POST["image_file1"];
$arr = array();
	if (!empty($_FILES["image_file1"])){
	array_push($arr, $_FILES["image_file1"]);
}
	if (!empty($_FILES["image_file2"])){
	array_push($arr, $_FILES["image_file2"]);
}
	if (!empty($_FILES["image_file3"])){
	array_push($arr, $_FILES["image_file3"]);
}
	if (!empty($_FILES["image_file4"])){
	array_push($arr, $_FILES["image_file4"]);
}
	if (!empty($_FILES["image_file5"])){
	array_push($arr, $_FILES["image_file5"]);
}

if (count($arr) == 0){
$error = "No images were selected to be uploaded";	
}
if (empty($_POST["album"]) && empty($_POST["nalbum"])){
$error = "Please either select an album to put the photos into or type in a new one";	
}
if (!empty($_POST["album"]) && !empty($_POST["nalbum"])){
$error = "Please only select an album to put the photos into <b>OR</b> type in a new one";	
}


if (!empty($_POST["album"]) || !empty($_POST["nalbum"])){
if(!empty($_POST["album"])){
	$album = $_POST["album"];
}
else
{
	$nalchk = "SELECT * FROM folders";
	$result2= mysql_query($nalchk);

			while ($row = mysql_fetch_assoc($result2)) {
				if($row["name"] = $_POST["nalbum"]){
					$error = "An album by this name allready exsists";
				}
			}
	if (empty($error)){	
	$album = $_POST["nalbum"];
	$na= "/photos/thumb/".$_POST["nalbum"];
	$nal = "INSERT INTO folders (`id`,`name`,`cover_thumb`) VALUES (NULL,'$album','$na')";
     	mysql_query($nal);
     	}
}
}

if(!empty($error)){
echo "The proccess could not me completed: ". $error;
}
else
{
foreach($arr as $var){
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//>>>>>>>>>>>>>>>>>>>>>>>>Upload the images >>>>>>>>>>>>>>>>>>>>>>>>>>>
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
unset($imagename);

if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;

if(!isset($_FILES["$var"]))
$error["$var"] = "An image was not found.";

$filename = basename($_FILES["$var"]['name']);
$maxsize = 2621440;
if(filesize($filename) > $maxsize)
$error["$var"] = "The image is too large, the image must be ". $maxsize ." bytes or smaller" ;
echo $error["$var"];

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

$orig = basename($_FILES["$var"]['name']);
$finfo = explode('.',$orig);
$filet = array_pop($finfo);

echo $filet."<br/>";

if($filet == "jpg" || $filet == "JPG" || $filet == "jpeg" || $filet == "JPEG"){
      //Do nothing at all
} else {
$error["$var"] = "Please make sure the image file is a .JPG" ;
echo $error["$var"];


};


//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



$imagename = basename($_FILES["$var"]['name']);
//echo $imagename;

if(empty($imagename))
$error["$var"] = "The name of the image was not found.";

if(empty($error))
{
$newimage = "photos/" . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES["$var"]['tmp_name'], $newimage);
$Imgurl = $newimage;

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

//+++++++++++++++++++++++++++++++Make Thumbnails for images++++++++++++++++++++++++++




    // load image and get image size
      $img = imagecreatefromjpeg( "photos/".$newimage );
      $width = imagesx( $img );
      $height = imagesy( $img );

      // calculate thumbnail size
      $new_width = 140;
      $new_height = floor( $height * ( $thumbWidth / $width ) );

      // create a new temporary image
      $tmp_img = imagecreatetruecolor( $new_width, $new_height );

      // copy and resize old image into new image
      imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

      // save thumbnail into a file
      imagejpeg( $tmp_img, "photos/thumb/". $newimage );
      $thmb = "photos/thumb/". $newimage;
    
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



$sql = "INSERT INTO photos (`id`,`url`,`thumb_url`,`folder`) VALUES (NULL,'$Imgurl','$thmb','$album')";
mysql_query($sql);

if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}}}}
?>
<!--Main page Contents -->
<form method="POST" enctype="multipart/form-data" name="image_upload_form" action="<?$_SERVER["PHP_SELF"];?>">
<tr>
<td width="200" valign="top">
Please Select Images to Upload:<br />
<input type="file" name="image_file1" size="20"/><br />
<input type="file" name="image_file2" size="20"/><br />
<input type="file" name="image_file3" size="20"/><br />
<input type="file" name="image_file4" size="20"/><br />
<input type="file" name="image_file5" size="20"/>
</td>
<td valign="top" align="left">Select a album to put the photos in:<br />
	<select name="album">
		<option value="">-</option>
		<?php

		$folerz = "SELECT * FROM folders";
		$fold= mysql_query($folerz);

		while ($row = mysql_fetch_assoc($fold)) {
		echo '<option value="'. $row["name"] .'">'. $row["name"] .'</option>';
		}

		?>
	</select><br />
	or Create a new one:<input type="text" name="nalbum" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Submit" name="Submit"/><br/>
</td>
</tr>

<?php include "data/footer.php"; ?>

Your form fields should be an array (note I explicitly entered index numbers to match what you are currently doing instead of allowing the index to automatically start at zero, which what most code examples will be showing)  -

 

   <input type="file" name="image_file[1]" size="20"/><br />
   <input type="file" name="image_file[2]" size="20"/><br />
   <input type="file" name="image_file[3]" size="20"/><br />
   <input type="file" name="image_file[4]" size="20"/><br />
   <input type="file" name="image_file[5]" size="20"/>

 

This will result in an array - $_FILES['image_file'] that you can simply iterate over in a loop instead repeating code to handle each file. There's actually a section in the php manual that shows this and it also shows the resulting $_FILES variables - http://www.php.net/manual/en/features.file-upload.multiple.php

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.