Reputation: 520
Before I start, I want to clarify that I did extensive research on this but none of the solutions work.
I have been create a app that allow to users upload your images to create an event.
I tried to upload this images to my server, and I received successfully in the response but when I check my directory whether the image is not there, I assume that did not uploaded.
Before I upload the image I check whether the image already exists in folder, to do this I have to create a function using curl to verify that, because function file_exists
did not working.
I don't know what I have to do more, to get this. P.S: I'm using backbone.js to build my application.
here my code: html:
<form id="uploadimage" action="" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file">
</form>
view.js
events: {
'click #guardar' : 'guardarEvento',
'click #guardarImagem' : 'guardarImagem',
'change input[type=file]' : 'uploadFile'
},
guardarImagem: function(){
var fileInput = document.getElementById('uploadimage');
console.log(new FormData(fileInput));
$.ajax({
url: "js/views/ajax_php_file.php", // Url to which the request is send
type: "POST", // Type of request to be send, called as method
data: new FormData(fileInput), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
contentType: false, // The content type used when sending data to the server.
cache: false, // To unable request pages to be cached
processData:false, // To send DOMDocument or non processed data file it is set to false
success: function(data){ // A function to be called if request succeeds
console.log(data);
}
});
},
ajax_php_file.php
<?php
if(isset($_FILES["file"]["type"])){
$validextensions = array("jpeg", "jpg", "png");
$temporary = explode(".", $_FILES["file"]["name"]);
$file_extension = end($temporary);
if ((($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/jpeg")
) && ($_FILES["file"]["size"] < 100000)//Approx. 100kb files can be uploaded.
&& in_array($file_extension, $validextensions)) {
if ($_FILES["file"]["error"] > 0){
echo "Return Code: " . $_FILES["file"]["error"] . "<br/><br/>";
}else{
if (file_exists("http://www.***/webtools/ondequemvaiver/agora/img/" . $_FILES["file"]["name"])){
echo $_FILES["file"]["name"] . " <span id='invalid'><b>already exists.</b></span> ";
}else{
$sourcePath = $_FILES['file']['tmp_name']; // Storing source path of the file in a variable
$targetPath = "http://www.***/webtools/ondequemvaiver/agora/img/".$_FILES['file']['name']; // Target path where file is to be stored
move_uploaded_file($sourcePath,$targetPath) ; // Moving Uploaded file
echo "<span id='success'>Image Uploaded Successfully...!!</span><br/>";
echo "<br/><b>File Name:</b> " . $_FILES["file"]["name"] . "<br>";
echo "<b>Type:</b> " . $_FILES["file"]["type"] . "<br>";
echo "<b>Size:</b> " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "<b>Temp file:</b> " . $_FILES["file"]["tmp_name"] . "<br>";
}
}
}else{
echo "<span id='invalid'>***Invalid file Size or Type***<span>";
}
}
?>
Upvotes: 2
Views: 1741
Reputation: 74216
You must use a system path when using move_uploaded_file()
rather than an http://
call.
I.e.:
$targetPath = "/var/user/httpdocs/webtools/ondequemvaiver/agora/img/...
or
$targetPath = "webtools/ondequemvaiver/agora/img/...
depending on the location of the script's execution.
You also need to make sure that the folder(s) has proper permissions in order to be written to.
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
Upvotes: 2