• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 47
  • Last Modified:

problem in upload images

i need to upload 9 images

this code is responsible for uploading all images

it works good but i have to problems :

1- slow page don't know why.
2- upload only 6 images not 9 and show error

// file vars
                    $path=$_FILES['products/']['tmp_name'];
                    $name=$_FILES['products/']['name'];
                    $size=$_FILES['products/']['size'];
                    $type=$_FILES['products/']['type'];
                    $error=$_FILES['products/']['error'];

					$j = 0; //Variable for indexing uploaded image

					$target_path = "products/"; //Declaring Path for uploaded images
				    for ($i = 0; $i < count($_FILES['file']['name']); $i++) {//loop to get individual element from the array

				        $validextensions = array("jpeg", "jpg", "png");  //Extensions which are allowed
				        $ext = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.)
				        $file_extension = end($ext); //store extensions in the variable

						$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1];//set the target path with a new name of image
				        $j = $j + 1;//increment the number of uploaded images according to the files in array

						if (($_FILES["file"]["size"][$i] < 200000) //Approx. 200kb files can be uploaded.
				          && in_array($file_extension, $validextensions)) {
				            if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {//if file moved to uploads folder

                                $sqlimage="INSERT INTO products_images (image_name,product_id) VALUES ('$target_path','$product_id')";

								$queryimage=mysqli_query($conn,$sqlimage);

				                echo $j. ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';
				            } else {//if file was not moved.
				                echo $j. ').<span id="error">please try again!.</span><br/><br/>';
				            }
				        } else {//if file size and file type was incorrect.
				            echo $j. ').<span id="error">***Invalid file Size (max 200kb) or Type (jpeg , jpg , png) ***</span><br/><br/>';
				        }
				    }

Open in new window


error on page :

1).Image uploaded successfully!.

2).Image uploaded successfully!.

3).Image uploaded successfully!.

4).Image uploaded successfully!.

5).Image uploaded successfully!.

6).Image uploaded successfully!.


Warning: move_uploaded_file(products/33b4179f8d76512f1a5fed14a0b8dfe0.jpgc949c46f9f94775d53ff1534a86f104d.jpg5fc3c7df9a6cc7485b31b06565ecb7f8.jpgb8a6e017c80539f138b97460d2649062.jpg61a6bdfb3732de16c90cd6c31d3b0289.jpg234c4ae6600450b57fc2bd143a7a98e9.jpg0d00018388fb3c8489743237544aa42f.jpg): failed to open stream: Invalid argument in C:\AppServ\www\*****\admin\addproducts.php on line 86

Warning: move_uploaded_file(): Unable to move 'C:\Windows\Temp\php9FED.tmp' to 'products/33b4179f8d76512f1a5fed14a0b8dfe0.jpgc949c46f9f94775d53ff1534a86f104d.jpg5fc3c7df9a6cc7485b31b06565ecb7f8.jpgb8a6e017c80539f138b97460d2649062.jpg61a6bdfb3732de16c90cd6c31d3b0289.jpg234c4ae6600450b57fc2bd143a7a98e9.jpg0d00018388fb3c8489743237544aa42f.jpg' in C:\AppServ\www\*****\admin\addproducts.php on line 86
7).please try again!.


Warning: move_uploaded_file(products/33b4179f8d76512f1a5fed14a0b8dfe0.jpgc949c46f9f94775d53ff1534a86f104d.jpg5fc3c7df9a6cc7485b31b06565ecb7f8.jpgb8a6e017c80539f138b97460d2649062.jpg61a6bdfb3732de16c90cd6c31d3b0289.jpg234c4ae6600450b57fc2bd143a7a98e9.jpg0d00018388fb3c8489743237544aa42f.jpg0d00018388fb3c8489743237544aa42f.jpg): failed to open stream: Invalid argument in C:\AppServ\www\******\admin\addproducts.php on line 86

Warning: move_uploaded_file(): Unable to move 'C:\Windows\Temp\php9FEE.tmp' to 'products/33b4179f8d76512f1a5fed14a0b8dfe0.jpgc949c46f9f94775d53ff1534a86f104d.jpg5fc3c7df9a6cc7485b31b06565ecb7f8.jpgb8a6e017c80539f138b97460d2649062.jpg61a6bdfb3732de16c90cd6c31d3b0289.jpg234c4ae6600450b57fc2bd143a7a98e9.jpg0d00018388fb3c8489743237544aa42f.jpg0d00018388fb3c8489743237544aa42f.jpg' in C:\AppServ\www\******\admin\addproducts.php on line 86
8).please try again!.


Warning: move_uploaded_file(products/33b4179f8d76512f1a5fed14a0b8dfe0.jpgc949c46f9f94775d53ff1534a86f104d.jpg5fc3c7df9a6cc7485b31b06565ecb7f8.jpgb8a6e017c80539f138b97460d2649062.jpg61a6bdfb3732de16c90cd6c31d3b0289.jpg234c4ae6600450b57fc2bd143a7a98e9.jpg0d00018388fb3c8489743237544aa42f.jpg0d00018388fb3c8489743237544aa42f.jpgd92b8e24565dc05f596aeedede0105d3.jpg): failed to open stream: Invalid argument in C:\AppServ\www\*****\admin\addproducts.php on line 86

Warning: move_uploaded_file(): Unable to move 'C:\Windows\Temp\php9FEF.tmp' to 'products/33b4179f8d76512f1a5fed14a0b8dfe0.jpgc949c46f9f94775d53ff1534a86f104d.jpg5fc3c7df9a6cc7485b31b06565ecb7f8.jpgb8a6e017c80539f138b97460d2649062.jpg61a6bdfb3732de16c90cd6c31d3b0289.jpg234c4ae6600450b57fc2bd143a7a98e9.jpg0d00018388fb3c8489743237544aa42f.jpg0d00018388fb3c8489743237544aa42f.jpgd92b8e24565dc05f596aeedede0105d3.jpg' in C:\AppServ\www\*******\admin\addproducts.php on line 86
9).please try again!.

Open in new window

addproducts.php
products.php
script.js
0
AHMED SAMY
Asked:
AHMED SAMY
  • 2
1 Solution
 
Leonidas DosasCommented:
The debugger shows you the problem.Your problem is at line 86
Warning: move_uploaded_file(products/33b4179f8d76512f1a5fed14a0b8dfe0.jpgc949c46f9f94775d53ff1534a86f104d.jpg5fc3c7df9a6cc7485b31b06565ecb7f8.jpgb8a6e017c80539f138b97460d2649062.jpg61a6bdfb3732de16c90cd6c31d3b0289.jpg234c4ae6600450b57fc2bd143a7a98e9.jpg0d00018388fb3c8489743237544aa42f.jpg): failed to open stream: Invalid argument in C:\AppServ\www\*****\admin\addproducts.php on line 86
Do a
var_dump($_FILES);

Open in new window

after the if condition (between lines 85 and 86) and post the output.
0
 
Julian HansenCommented:
Look at line 10 - where you initialise $target_path

Now look at line 17
$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1];

Open in new window

Do you see what you are doing? On each iteration of the for loop you are adding the next file name to the path.

Look at the warning you received
products/33b4179f8d76512f1a5fed14a0b8dfe0.jpgc949c46f9f94775d53ff1534a86f104d.jpg5fc3c7df9a6cc7485b31b06565ecb7f8.jpgb8a6e017c80539f138b97460d2649062.jpg61a6bdfb3732de16c90cd6c31d3b0289.jpg234c4ae6600450b57fc2bd143a7a98e9.jpg0d00018388fb3c8489743237544aa42f.jpg
                                         ^^^^                                ^^^^                                ^^^^                                ^^^^

Open in new window

Your target_path is getting longer and longer
Instead do this
Line 10
$base_path = "products/";

Open in new window

And on line 17 do this
$target_path = $base_path . md5(uniqid()) . "." . $ext[count($ext) - 1];

Open in new window

1
 
AHMED SAMYownerAuthor Commented:
thanks you very much you are very clever man
0
 
Julian HansenCommented:
Thank you and you are most welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now