Php 将图像上载到服务器不工作
在我开始之前,我想澄清一下,我对此做了广泛的研究,但没有一个解决方案有效 我已经创建了一个应用程序,允许用户上传你的图片来创建一个活动 我尝试将此图像上载到我的服务器,并在响应中成功接收到,但当我检查我的目录是否不存在该图像时,我假设没有上载该图像 在上传图像之前,我检查图像是否已经存在于文件夹中,为此,我必须使用curl创建一个函数来验证,因为函数Php 将图像上载到服务器不工作,php,curl,file-upload,upload,image-uploading,Php,Curl,File Upload,Upload,Image Uploading,在我开始之前,我想澄清一下,我对此做了广泛的研究,但没有一个解决方案有效 我已经创建了一个应用程序,允许用户上传你的图片来创建一个活动 我尝试将此图像上载到我的服务器,并在响应中成功接收到,但当我检查我的目录是否不存在该图像时,我假设没有上载该图像 在上传图像之前,我检查图像是否已经存在于文件夹中,为此,我必须使用curl创建一个函数来验证,因为函数file\u exists不起作用 我不知道我还要做什么才能得到这个。 附言:我正在使用backbone.js构建我的应用程序 这是我的代码: ht
file\u exists
不起作用
我不知道我还要做什么才能得到这个。
附言:我正在使用backbone.js构建我的应用程序
这是我的代码:
html:
ajax\u php\u 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>";
}
}
?>
在使用http://
调用时,必须使用系统路径
即:
或
取决于脚本执行的位置
您还需要确保文件夹具有适当的权限才能写入
添加到文件的顶部,这将有助于查找错误
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
是否已将错误检查到apache日志文件中?您的$targetPath看起来很奇怪,例如,$targetPath=“http://
不应该是http调用,而是指向的路径。$targetPath=“/var/user/httpdocs/webtools/ondequemvaiver/agora/img/
。阅读手册@Fred ii-I更改目标路径但仍不工作:/n确保文件夹具有正确的写入权限设置。在打开PHP标记后立即在文件顶部添加错误报告,例如我不太擅长Ajax/JS,但这更改输入[type=file]:“uploadFile”是第二个参数。如果这是一个“id”,那么这不应该是根据id=“file”
更改输入[type=file]:“file”
?
<?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>";
}
}
?>
$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