PHP - Uploaded file not sending to file directory -


on website, allow users submit files , sent database , file directory, devfiles, created. sends database fine, when send directory, never sends , error message created see if sends or not. believe problem

if(is_file($dir.'/'.$file_name)==false){         //code...         } 

but tried change condition didn't work. want is, send file submitted file directory on hand created. here code

php

$query = "insert pack_screenshots(pack_id, file_name, file_tmp)values(:packid, :file_name, :file_tmp)"; $stmtfileupload = $handler->prepare($query); $errors = array();  foreach($_files['file']['tmp_name'] $key => $error){  if ($error != upload_err_ok) {     $errors[] = $_files['file']['name'][$key] . ' not uploaded.';     continue;     }  $file_tmp = file_get_contents($_files['file']['tmp_name'][$key]); $file_name = addslashes(trim($_files['file']['name'][$key]));     try{  $stmtfileupload->bindparam(':packid', $packid, pdo::param_str); $stmtfileupload->bindparam(':file_name', $file_name, pdo::param_str); $stmtfileupload->bindparam(':file_tmp', $file_tmp, pdo::param_str);  $dir = "devfiles";  if(is_dir($dir)==false){      mkdir($dir, 0700);  }  if(is_file($dir.'/'.$file_name)==false){       if(!move_uploaded_file($file_tmp,$dir.'/'.$file_name)){                 die("file didn't send!");           }          }else{           $_session['invalid'] = true;         header("location: developer_invalid.php");         exit;         }         $stmtfileupload->execute();         $_session['thankyou'] = true;         header("location: developerupload_thankyou.php");         exit;          }catch(pdoexception $e){              $errors[] = $file_name . 'not saved in db.';             echo $e->getmessage();                     }                  } 

php documentation bool move_uploaded_file ( string $filename , string $destination )

you did :

move_uploaded_file($file_tmp,$dir.'/'.$file_name) 

move_uploaded_file expecting $file_tmp path tmp file used

$file_tmp = file_get_contents($_files['file']['tmp_name'][$key]); 

so $file_tmp no longer path content self solve upload problem use tmp file path instead.

if(!move_uploaded_file($_files['file']['tmp_name'][$key],$dir.'/'.$file_name)){ 

also, should remove addslashes() on file name because create unexpected results. instead, can sanitize filename using this:

$file_name = preg_replace("/[^a-z0-9\.]/", "_", strtolower($_files['file']['name'][$key])); 

you should consider adding random number file name users don't overwrite other users files have same name: me.png common avatar example. safer save

$filename = strtotime("now")."_me.png"; 

one last thing, using is_file() can cause problems in cases

note: because php's integer type signed , many platforms use 32bit integers, filesystem functions may return unexpected results files larger 2gb.

use file_exists() instead


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -