正在上传图片 - 错误

时间:2015-07-24 20:05:47

标签: php html twitter-bootstrap file upload

我用HTML创建一个页面,将图像传递给PHP脚本,然后将脚本上传到服务器上的目录。

这是我的HTML表单:

<form role="form" action="upload.php" method="post" enctype="multipart/form-data">
<div class="form-group">
  <div class="row" style="margin-top: 125px;">
    <div class="col-sm-4" style="border-style: solid; border-width: medium; border-color: black; height: 206px; width: 156px; ">
      <img id="blah" src="#" alt="your image" style="margin-left:-15px" />
    </div>
    <div class="col-sm-7">
      <h3 class="text-center" style="margin-top: 0px">Choose your picture:</h3>
      <input type='file' onchange="readURL(this);" class="hidden" id="fileToUpload" name="fileToUpload"/>
      <label for="fileToUpload" class="btn btn-info col-sm-12" style="margin-top: 60px">Browse</label>
      <a class="btn btn-warning col-sm-5" style="margin-top: 15px" href="#">Skip</a>
      <div class="col-sm-2"></div>
      <button class="btn btn-success col-sm-5" style="margin-top: 15px" type="submit">Upload</button>
    </div>
  </div>
</div>
</form>

这就是它的样子: http://oi60.tinypic.com/15xspjn.jpg

这是来自upload.php的PHP代码,我是从w3schools获得的:

 <?php
$target_dir = "../uploads/avatars/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
} else {
    echo "File is not an image.";
    $uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
    echo "Sorry, there was an error uploading your file.";
}
}
?>

我收到&#34;抱歉,上传文件时出错。&#34;上传图片时

任何想法为什么?

P.S。我已经&#34; file_uploads = On&#34;在我的PHP配置中。

1 个答案:

答案 0 :(得分:0)

正如您的if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) 所说:

move_uploaded_file

这就是说,只要在PHP函数$target_file中出现,就会调用它。 有关如何以及何时发生此错误的信息,您必须转到http://www.scala-sbt.org/0.13/docs/Faq.html#How+can+I+create+a+custom+run+task%2C+in+addition+to+%3F

对于我们(堆栈溢出的社区),它难以检测为什么它说它是因为它可能是多个问题,我们不确定在发生此错误之前您做了什么。 你能详细说明一下吗?

此功能通常用于检查POSTED文件指定是否为合法标记。 因此,如果$target_dir . basename($_FILES["fileToUpload"]["name"]); $target_dir = "../uploads/avatars/" $_FILES["fileToUpload"]["name"]if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)),则可能存在多个问题,例如目录无法访问或不存在。 您需要确保此目录存在。

下一个问题可能是你要覆盖现有文件,但由于某种原因无法做到这一点(使用if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_dir . 'test' . '.' . $imageFileType))并不是我理想的选择,我会做一些事情,比如花时间和今天的日期。即如果你不需要该文件是一个特定的名称)

如果有任何帮助,请告诉我:)。

编辑:您还可以尝试将objectAt更改为var order = this.get('order'); var orderItems = order.get('orderItems'); for (i = 0; i < orderItems.get('length'); i++) { orderItems.objectAt(i).set('price', 1000); } ,以测试是否有效。