在开始之前,我想澄清一下,我对此进行了广泛的研究,但没有一种解决方案起作用。
我已经创建了一个应用程序,允许用户上传您的图像来创建事件。
我尝试将这些图像上传到服务器,并且在响应中成功收到,但是当我检查目录中是否不存在该图像时,我认为该图像没有上传。
在上载图像之前,我检查图像是否已存在于文件夹中,为此,我必须使用curl创建一个函数来验证这一点,因为该函数file_exists
无法正常工作。
我不知道我要做些什么才能得到这个。PS:我正在使用bone.js来构建我的应用程序。
这是我的代码: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>";
}
}
?>
使用move_uploaded_file()
而不是http://
调用时,必须使用系统路径。
IE:
$targetPath = "/var/user/httpdocs/webtools/ondequemvaiver/agora/img/...
或者
$targetPath = "webtools/ondequemvaiver/agora/img/...
取决于脚本执行的位置。
您还需要确保文件夹具有适当的权限才能写入。
将错误报告添加到文件顶部,这将有助于发现错误。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
旁注:错误报告仅应在登台进行,而不应在生产过程中进行。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句