Jump to content
php.lv forumi
Sign in to follow this  
ziedinjsh

upload un foreach

Recommended Posts

Sveiki.

 

šis ir kods:

if(isset($_POST['upload']))
{

foreach ($_FILES["pic"]["error"] as $key => $error){
   	if ($error == UPLOAD_ERR_OK){
if(isset($_FILES['pic']['name']) && !empty($_FILES['pic']['name'])){
$name = $_FILES['pic']['name'];
$tmp_name = $_FILES['pic']['tmp_name'];
$file_size = $_FILES['pic']['size'];
$file_size_limit = 5242881;
$type = $_FILES['pic']['type'];
$types_allowed = array('image/jpeg', 'image/jpg', 'image/gif', 'image/png');
$location = 'uploads/';
$normal = 200;
$small = 50;

$errors = array();
$counter = 0;
do{
 $rand_filename = rand(10000,99999).'.jpeg';
 $uniq_name = $location.$rand_filename;
 $location_contents = scandir($location);
 $counter++;
 if($counter>99999){
  echo 'Can not find a unique name.<br/>';
  $errors[] = '1';
 }
}
while(in_array($rand_filename, $location_contents));



if($file_size>$file_size_limit){
 echo 'Image is to big - 5Mb<br/>';
 $errors[] = '1';
}


if(!in_array($type, $types_allowed)){
 echo 'Onely image file';
 $errors[] = '1';
}


if(empty($errors)){
 if(move_uploaded_file($tmp_name, $uniq_name)){


	function resize_image($width_height, $folder_path){

 	global $location, $rand_filename, $image_path;

 	if(isset($rand_filename, $location)){

  	$image_path = $location.$rand_filename;


  	if(file_exists($image_path)){


   	$image_size = getimagesize($image_path);
   	$image_width = $image_size[0];
   	$image_height = $image_size[1];

   	if($old_image = @imagecreatefromjpeg($image_path)){

   	}elseif($old_image = @imagecreatefromgif($image_path)){

   	}else if($old_image = @imagecreatefrompng($image_path)){

   	}
   	else{
    	echo 'Image upload error.<br/>';
    	return false;
    	break;
   	}


    	$offset = strpos($rand_filename, '.');
          	$result_image_name = $folder_path.$rand_filename;



   	if($image_height>$image_width){
    	$greater_axe = $image_height;
    	$smaller_axe = $image_width;
   	}
   	else{
    	$greater_axe = $image_width;
    	$smaller_axe = $image_height;
   	}



   	$x = ($image_width-$smaller_axe)/2;
   	$y = ($image_height-$smaller_axe)/2;



   	$image_croped = imagecreatetruecolor($width_height, $width_height);
   	if(@!imagecopyresampled($image_croped, $old_image, 0, 0, $x, $y, $width_height, $width_height, $smaller_axe, $smaller_axe)){
   	echo 'Function error.<br/>';
   	return false;
   	break;
   	}



   	if(@imagejpeg($image_croped, $result_image_name)){

    	return true;
   	}
   	else{

    	echo 'Image upload error.<br/>';
    	return false;
    	break;
   	}
  	}
  	else{
   	echo 'Picture does not exist.<br/>';
   	return false;
  	}
 	}
 	else{

  	echo 'Upload error.<br/>';
  	return false;
 	}

	}


	if(resize_image($normal, 'uploads/normal/') && resize_image($small, 'uploads/small/')){

	echo 'Viss ok!';



	}
	else{
 	//unlink($image_path);
	}


}
 else{
  echo 'Function error, please try later.<br/>';
 }
}
}else{
echo 'Please select image';
}

   	}
}

}

 

Izvēlos bildi, palaižu kodu un man izmet kļūdu ziņojumus:Image is to big - 5Mb un Onely image file. Bilde ir 2.6 mb liela un ir jpg formāta bilde

Share this post


Link to post
Share on other sites


$file_size>$file_size_limit

echo $file_size.' > '.$file_size_limit;

Izdrukā tad redzēsi kādēļ tiek attēla izmēra kļūda izdrukāta.

Edited by reiniger

Share this post


Link to post
Share on other sites

malinens paldies! Mēģinot augšupielādēt: Image is to big - 5Mb

 

Notice: Array to string conversion in D:\Server\xampp\htdocs\projekti\up\index.php on line 112

Array < 5242881Onely image file

Edited by ziedinjsh

Share this post


Link to post
Share on other sites

īsti nesaprotu, kā var būt par lielu un nebūt bilžu tips, ja viss atbilst

Edited by ziedinjsh

Share this post


Link to post
Share on other sites

šobrīd es meiģinu ar vienu bildi. Izskatās, ka nemaz kods nepaņem bildes izmēru, jo izvadot $_FILES['pic']['size'] > $file_size_limit izvadās tikai file size limit bet file size vieta ir tukšums!

Share this post


Link to post
Share on other sites

Esi aizmirsis indeksu masīva elementiem. Iemet pašā augšā un salīdzini ar savu rakstīto.

echo '<pre>';

print_r($_FILES);

echo '</pre>';

Share this post


Link to post
Share on other sites

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Multiple File Upload with jQuery and PHP</title>
<script src="jquery-1.8.1.min.js" type="text/javascript" language="javascript"></script>
<script src="jquery.MultiFile.js" type="text/javascript" language="javascript"></script>
</head>
<body>
<form action="index.php" method="post" enctype="multipart/form-data">
<input type="file" name="pic[]" class="multi" multiple />
<input type="submit" name="upload" value="Upload" />
</form>
</body>
</html>

Share this post


Link to post
Share on other sites

Esi aizmirsis indeksu masīva elementiem. Iemet pašā augšā un salīdzini ar savu rakstīto.

echo '<pre>';

print_r($_FILES);

echo '</pre>';

Array( [pic] => Array ( [name] => Array ( [0] => 1.jpg ) [type] => Array ( [0] => image/jpeg ) [tmp_name] => Array ( [0] => D:\Server\xampp\tmp\phpEB37.tmp ) [error] => Array ( [0] => 0 ) => Array ( [0] => 532748 ) ))

Image is to big - 5Mb

 

Notice: Array to string conversion in D:\Server\xampp\htdocs\projekti\up\index.php on line 116

Array > 5242881Onely image file

Edited by ziedinjsh

Share this post


Link to post
Share on other sites

Tava problēma ir šeit:

<input type="file" name="pic[]" class="multi" multiple />

Tu augšupielādē vienu failu izmantojot vairāku failu augšupielādes tehniku - norādot name ne kā unikālu masīva ierakstu, bet kā masīvu - [] pēc pic. Skripts acīmredzot paredzēts viena faila augšupielādei.

A: aizvāc []

B: pārraksti skriptu, lai būtu vairāku failu atbalsts.

 

 

Augšupielādējot failus ar šādu formu tu saņem ne $_FILES['pic'] masīvu ar informāciju par failu, bet gan $_FILES['pic'] kā inkrementālu masīvu, kur katrs ieraksts satur faila informāciju.

Edited by F3llony

Share this post


Link to post
Share on other sites

šī koda daļa taču ir kā vairāku failu augšupielāde:

foreach ($_FILES["pic"]["error"] as $key => $error) {
   	if ($error == UPLOAD_ERR_OK) {
       	if (isset($_FILES['pic']['name']) && !empty($_FILES['pic']['name'])) {
           	$name = $_FILES['pic']['name'];
           	$tmp_name = $_FILES['pic']['tmp_name'];
           	$file_size = $_FILES['pic']['size'];
           	$type = $_FILES['pic']['type'];

           	$errors = array();
           	$counter = 0;
           	do {
               	$rand_filename = rand(10000, 99999) . '.jpeg';
               	$uniq_name = $location . $rand_filename;
               	$location_contents = scandir($location);
               	$counter++;
               	if ($counter > 99999) {
                   	echo 'Can not find a unique name.<br/>';
                   	$errors[] = '1';
               	}
           	} while (in_array($rand_filename, $location_contents));



           	if ($file_size > $file_size_limit) {
               	echo 'Image is to big - 5Mb<br/>';
               	echo $_FILES['pic']['size'].' > '.$file_size_limit;
               	$errors[] = '1';
           	}


           	if (!in_array($type, $types_allowed)) {
               	echo 'Onely image file';
               	$errors[] = '1';
           	}


           	if (empty($errors)) {
               	if (move_uploaded_file($tmp_name, $uniq_name)) {


                   	if (resize_image($normal, 'uploads/normal/') && resize_image($small, 'uploads/small/')) {

                       	echo 'Viss ok!';
                   	} else {
                       	//unlink($image_path);
                   	}
               	} else {
                   	echo 'Function error, please try later.<br/>';
               	}
           	}
       	} else {
           	echo 'Please select image';
       	}
   	}
}

 

Meiģināju vairākus failus: Tās pašas kļūda, bet nu atkārtoti.

 

aizvācot [] :Array( [pic] => Array ( [name] => 1.jpg [type] => image/jpeg [tmp_name] => D:\Server\xampp\tmp\php6887.tmp [error] => 0 => 532748 ))

 

Warning: Invalid argument supplied for foreach() in D:\Server\xampp\htdocs\projekti\up\index.php on line 91

 

aizvācot foreach rindu. viss ir ok.

 

šādi:

if(isset($_POST['upload'])){
$uploaddir = 'uploads/';
foreach ($_FILES["pic"]["error"] as $key => $error)
{
   	if ($error == UPLOAD_ERR_OK)
   	{
       	$tmp_name = $_FILES["pic"]["tmp_name"][$key];
       	$name = $_FILES["pic"]["name"][$key];
       	$uploadfile = $uploaddir . basename($name);

       	if (move_uploaded_file($tmp_name, $uploadfile))
       	{
           	echo "ir.<br/>";
       	}
       	else
       	{
           	echo "nav.<br/>";
       	}
   	}
}


}

arī viss izdodās.. Augšupielādē tik failus cik esmu izvēlējies, bet protams vienā eksemplārā un bez sīkbildēm

Edited by ziedinjsh

Share this post


Link to post
Share on other sites

To pirmo koda blāķi aizvāc vispār. Bullšits tas ir, ko tu tur sadarīji.

1. Uztaisi funkciju, kura izveido thumbneilus.

2. ciklē cauri $_FILES['pic'] MASĪVAM un piemēro funkciju katram failam, kas tur ir.

Share this post


Link to post
Share on other sites

izdarīju tā.

thumb:

http://paste.php.lv/46eca0ebb4e197d1d3c0ec40e8091ec1?lang=php

upload:

http://paste.php.lv/40535cd3cd7f03c0c4679d5e5b52ba9a?lang=php

html

<html>
<head>
<meta charset="utf8" />
<title>Administrācija</title>
<script type="text/javascript" src="js/jquery-1.8.1.min.js"></script>
<script type="text/javascript" src="js/jquery.MultiFile.js"></script>
</head>
<body>
<?php
include("../misc/dbase.php");
$p = isset($_GET['p']) ? $_GET['p'] : '';
if($p==''){
echo '
<form action="process/photo.process.php" method="post" enctype="multipart/form-data">
<select name="cat" style="width:400px;height:30px;border:1px solid #ccc;font-size:20px;margin-bottom:10px;">
<option value=""> - Izvēlies - </option>
<option value="portreti">Portreti</option>
<option value="berni">Bērni</option>
<option value="kazas">Kāzas</option>
<option value="daba">Daba</option>
</select>
<input type="file" name="pic[]" class="multi" style="width:400px;height:30px;border:1px solid #ccc;font-size:20px;" multiple/>
<input type="submit" name="upload" value="Upload" style="width:150px;height:30px;border:1px solid #ccc;background:#f2f2f2;font-size:16px;margin-top:10px;" />
</form>
';



}
?>
</body>
</html>

 

Izvēlos bildi/bildes. Fails augšupielādējas bet atveras photo.process.php un viss. Balta lapa (uz lokālo serveri iet, bet uz hosta serveri neiet)

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
Sign in to follow this  

×
×
  • Create New...