简体   繁体   中英

php file upload returning validity of file and file path

I have a webpage that contains a form with several text fields and an optional file upload.

When the user clicks the submit button I do the following:

  • check if a file has been selected for upload
  • if so, upload the file
  • when the file has finished uploading, submit the other form data

My question is this. At the moment, my php server script checks the file as to whether or not it is valid. As of now, I have no way of knowing whether or not the file is valid before submitting the form data. I would like to be able to know that the file was uploaded, valid and also the path where the file was saved.

var xmlhttp=new XMLHttpRequest();
var formData = new FormData();
formData.append("thefile", file);   
xmlhttp.addEventListener("load", uploadComplete, false);
xmlhttp.open("POST","uploadfile.php",true);
xmlhttp.setRequestHeader("enctype", "multipart/form-data");
xmlhttp.send(formData);

.

 function uploadComplete(evt) {
    alert("image upload complete");
    submitForm();
 }

I basically want to send information from php that can be read from my function uploadComplete

An easy way to do this is to make a echo in your PHP script, like this for example :

echo json_encode({YOUR INFORMATIONS});

After in uploadComplete json_decode the return and you will be able to use the informations directly in javascript.

Also I suggest you check if the file has been uploaded with PHP. Just the function file_exists, here's an example:

   if (file_exists($path)) {
   echo 'ok';
   }else{ 
   echo 'file not loaded';
   }

It often happens that the upload did not go wrong!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM