7 Replies - 452 Views - Last Post: 14 July 2013 - 02:43 AM Rate Topic: -----

#1 Yeronox  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 102
  • Joined: 12-March 12

Multiple File Uploader (multiple errors, too)

Posted 13 July 2013 - 06:36 AM

Hi guys!
I've coded this multiple file uploaded today but it doesn't work:
  • It uploads only one item even if I select more than 1
  • It doesn't echo the error if the file is greater than 3 MB
  • It doesn't post anything to the DB

About the DB, I wanted to make a login form, too, so if you were a registered user you could manage your images, if not you could just upload and view your images. And about this idea, I don't have anything in mind on how to do it.
Anyway, here's my code:
index.php
<form action="uploader.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="files[]" multiple/>
    <input type="submit"/>
</form>

func.php
<?php
	
	$config = array('host'=>'localhost',
					'username'=>'root',
					'password'=>'');
	function connect_to_db($config)
	{
		try {
			$conn = new PDO('mysql:host=localhost;dbname=practise', $config['username'], $config['password']);
			$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			return $conn;
		} catch (Exception $e) {return false;}
	}

	function query($query, $binds, $conn)
	{
		$stm = $conn->prepare($query);
		$stm->excecute($binds);
		return ($stm->rowCount()>0) ? $stm : false;
	}?>

uploader.php
<?php
	require 'func.php';
	$conn = connect_to_db($config);
	if (!$conn) die('Cannot connect');
	//if (!isset($_FILES['files'])) die('You have to select at least one image.');
	$errors = array();
	if(isset($_FILES['files'])){
		
		foreach( $_FILES['files']['tmp_name'] as $key => $tmp_name )
		{
			$file_name = $key.$_FILES['files']['name'][$key];
			$file_size = $_FILES['files']['size'][$key];
			$file_tmp  = $_FILES['files']['tmp_name'][$key];
			$file_type = $_FILES['files']['type'][$key];
			if($file_size > 3145728)
			  {$errors[] = "The size must be equal or less than 3 MB. <br>";}
		}
		$user_id = 1;
		$extensions = array('jpg', 'jpeg', 'png', 'bmp', 'gif');
		$file_ext = explode('.', $file_name)	;
		$file_ext = end($file_ext);  
		$file_ext = strtolower($file_ext); 

		if(in_array($file_ext, $extensions)===false)
			  { $errors[] = "Your image cannot be uploaded. The extension is not supported."; }
		
		$query_upload = "INSERT INTO `practise`.`upload` (`FILE_NAME`, `FILE_SIZE`, `FILE_TYPE`) VALUES('$file_name','$file_size','$file_type'); " ;
		$upload_folder = 'img_data';

		if(empty($errors)==true)
		{		
		 if(!is_dir($upload_folder)) {
	    	 mkdir("$upload_folder", 0700);
		 }
		
			if(is_dir("$upload_folder/".$file_name)==false){
				move_uploaded_file($file_tmp,"img_data/".$file_name);
			} else{
				$new_dir="img_data/".$file_name.time();
	            rename($file_tmp,$new_dir) ;
			}

			// try {
			// 	$query = query($query_upload,
			// 		array('USER_ID'=>1,
			// 			  'FILE_NAME'=>$file_name,
			// 			  'FILE_SIZE'=>$file_size,
			// 			  'FILE_TYPE'=>$file_type),
			// 		$conn
			// 		);
			// 	if($query) return $query->fetchAll();
			// } catch(Exception $e) {return false;}
			
			mysql_query($query_upload);

			echo $success = "Picture uploaded successfuly. <br>";
		} else {
			$error_cant_query = 'The size exceeded the max size or the extension was not allowed.';
			print_r($errors);
			//echo "<br>$error_cant_query";
			    }
	}
?>

Ask me any detail you want (I'm fairly new to PHP)

Is This A Good Question/Topic? 0
  • +

Replies To: Multiple File Uploader (multiple errors, too)

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3490
  • View blog
  • Posts: 11,902
  • Joined: 12-December 12

Re: Multiple File Uploader (multiple errors, too)

Posted 13 July 2013 - 07:03 AM

I've only had a quick look but your only loop is storing values in variables $file_name, etc.. Each time through the loop these values are over-written, so they will only contain the latest values.

I'm not sure that your use of $key is correct, within this loop. Well, this expression $key.$_FILES in particular looks wrong.

To check the size, and anything else, use lots of echo, print_r or var_dump statements. You need to use these features extensively to help you debug.

print_r will produce a viewable output of a variable or array. View the source for your page so that you can read this output more clearly.

Print out your $query_upload string as well and attempt to execute this statement in PHPMyAdmin or similar.

This post has been edited by andrewsw: 13 July 2013 - 07:08 AM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3490
  • View blog
  • Posts: 11,902
  • Joined: 12-December 12

Re: Multiple File Uploader (multiple errors, too)

Posted 13 July 2013 - 07:17 AM

A quick check suggests that the file-size is return in Kb, so your check value is too large. Echo-ing your variable $file_size will help to confirm this.
Was This Post Helpful? 0
  • +
  • -

#4 Yeronox  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 102
  • Joined: 12-March 12

Re: Multiple File Uploader (multiple errors, too)

Posted 13 July 2013 - 12:24 PM

Here's my new upload.php file
Again, it doesn't choose multiple files and it doesn't post anything to the DB, though the connection works.
<?php
	require 'db.php';
	$conn = connect_db($config);
	if(!$conn) die('Cannot connect');
	function upload_multiple_file($file, $file_dir = 'user_files')
	{
		$overwrite = 0;
		$allowed_file_type = array('jpg','jpeg','png','bmp','gif');
		$max_file_size_check=1;
    	$max_file_size = 3145728;

    	foreach($_FILES['files']['name'] as $fkey => $fname)
    	{
    		$ext = pathinfo($fname, PATHINFO_EXTENSION);
    		if (!in_array($ext, $allowed_file_type))
    		{
    			return "Unsupported file format.";
    			break;
    		}
    	}
    	foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name)
    	{
    		$file_name = $_FILES['files']['name'][$key];
    		$file_size = $_FILES['files']['size'][$key];
    		$file_type = $_FILES['files']['type'][$key];
    		$file_tmp_name = $_FILES['files']['tmp_name'][$key];

    		if($max_file_size_check != 1)
    		{
    			if($file_size > $max_file_size)
    			{
    				$fsize = $max_file_size/1048576;
    				return "Files cannot be larger than $fsize MB. ";
    				break;
    			}
    		}
    		if(!is_dir($file_dir)) {
	    	 mkdir("$file_dir", 0700);
		 }
		
			if(is_dir("$file_dir/".$file_name)==false){
				move_uploaded_file($file_tmp_name,"$file_dir/".$file_name);
			} else{
				$new_dir="img_data/".$file_name.time();
	            rename($file_tmp,$new_dir) ;
			}
    		$user_id = 1; // to implement with $_GET
    		$query_ok_upload = "INSERT INTO uploads_table('user_id', 'file_name', 'file_size', 'file_type') VALUES('$user_id','$file_name','$file_size','$file_type'); ";
    		mysql_query($query_ok_upload);
    		return "Success";
    	}

	}
	if (isset($_FILES['files']))
	{
		$result = upload_multiple_file($_FILES['files']);
		echo $result;
	}

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3490
  • View blog
  • Posts: 11,902
  • Joined: 12-December 12

Re: Multiple File Uploader (multiple errors, too)

Posted 13 July 2013 - 01:27 PM

Did you do all the debugging with echo, etc., that I suggested?

"INSERT INTO uploads_table('user_id', 'file_name', 'file_size', 'file_type')

These need to be back-ticks, not apostrophes. They were in your original code so I don't know why you changed them.
Was This Post Helpful? 0
  • +
  • -

#6 Yeronox  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 102
  • Joined: 12-March 12

Re: Multiple File Uploader (multiple errors, too)

Posted 14 July 2013 - 12:01 AM

It shouldn`t be a problem; anyway I`ve changed it and it still doesn`t work, not the mySQL nor some kind of error.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3490
  • View blog
  • Posts: 11,902
  • Joined: 12-December 12

Re: Multiple File Uploader (multiple errors, too)

Posted 14 July 2013 - 12:34 AM

Add the following to the top of your script to display all error messages:

error_reporting(E_ALL);
ini_set('display_errors', '1');

Was This Post Helpful? 0
  • +
  • -

#8 Yeronox  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 102
  • Joined: 12-March 12

Re: Multiple File Uploader (multiple errors, too)

Posted 14 July 2013 - 02:43 AM

Added, but it doesn`t show any error to me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1