Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to store multiple images in an array before inserting to mysql
    text
    copied!<p>I have an upload function that is working with one or multiple files able to upload to a folder and then insert an index to mysql. How do I store multiple images as an array?</p> <pre><code>//This upload code to allow users to upload specific files &lt;?php $user_id = $_SESSION['lt_user_id']; $user_code = $_SERVER['AUTH_USER']; if(isset($_FILES['files'])){ $errors= array(); foreach($_FILES['files']['tmp_name'] as $key =&gt; $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 &gt; 2097152){ $errors[]='File size must be less than 2 MB'; } $host="server.mysql.com"; $username="user"; $password="password"; $db_name="db01"; $tbl_name="upload_data"; // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // Insert data into mysql $sql="INSERT into $tbl_name (ID, USER_CODE, FILE_NAME, FILE_SIZE, FILE_TYPE) VALUES('$user_id','$user_code','$file_name','$file_size','$file_type'); "; $desired_dir="../backup1Jan2013/images/"; if(empty($errors)==true){ if(is_dir($desired_dir)==false){ mkdir("$desired_dir", 0700); // Create directory if it does not exist } if(is_dir("$desired_dir/".$file_name)==false){ move_uploaded_file($file_tmp,"$desired_dir/".$file_name); }else{ // rename the file if another one exist $new_dir="$desired_dir/".$file_name.time(); rename($file_tmp,$new_dir) ; } mysql_query($sql); }else{ print_r($errors); } } if(empty($error)){ echo "Success"; } } // close connection mysql_close(); ?&gt; &lt;form action="" method="POST" enctype="multipart/form-data"&gt; &lt;input type="file" name="files[]" multiple/&gt; &lt;input type="submit"/&gt; &lt;/form&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload