Note that there are some explanatory texts on larger screens.

plurals
  1. POUploading file by User Id in mysql?
    primarykey
    data
    text
    <p>I have two tables one is for users and one is for images All what I would like to achieve is that if I log in I can be able to upload images for a user Since no user will be allowed to upload images I wanna upload images for them and each and every user will be able to view his own images . I have a working code to upload multiple images but now How to assign them to a specific user . the code bellow just uploads multiple images .</p> <pre><code>&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; &lt;?php $username = (isset ($_POST['username'])); if(isset($_FILES['files'])){ $query = "INSERT into tish_images(`FILE_NAME`,`FILE_SIZE`,`FILE_TYPE`) VALUES(:FILE_NAME,:FILE_SIZE,:FILE_TYPE)"; $stmt = $con-&gt;prepare($query); $errors= array(); foreach($_FILES['files']['tmp_name'] as $key =&gt; $error ){ if ($error != UPLOAD_ERR_OK) { $errors[] = $_FILES['files']['name'][$key] . ' was not uploaded.'; continue; } $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'; continue; } try{ $stmt-&gt;bindParam( ':FILE_NAME', $file_name , PDO::PARAM_STR ); $stmt-&gt;bindParam( ':FILE_SIZE', $file_size, PDO::PARAM_STR ); $stmt-&gt;bindParam( ':FILE_TYPE', $file_type, PDO::PARAM_STR ); $stmt-&gt;execute(); $desired_dir="image_uploads"; if(is_dir($desired_dir)==false){ mkdir($desired_dir, 0700);// Create directory if it does not exist } if(is_file($desired_dir.'/'.$file_name)==false){ move_uploaded_file($file_tmp,$desired_dir.'/'.$file_name); }else{ //rename the file if another one exist $new_file=$desired_dir.'/'.$file_name.time(); move_uploaded_file($file_tmp,$new_file) ; } }catch(PDOException $e){ $errors[] = $file_name . 'not saved in db.'; echo $e-&gt;getMessage(); } } if(empty($error)){ echo "Success"; } } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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