【问题标题】:move_upload_file not workingmove_uploaded_file 不工作
【发布时间】:2016-10-31 11:36:29
【问题描述】:

您好,我正在尝试将图像上传到我的数据库和文件夹以及表单中的其他元素,除了我无法让文件出现在上传文件夹中之外,一切正常。这是我的代码:

if(isset($_POST['submit'])){
    //This gets all the other information from the form
    $name = $_POST['name'];
    $description = $_POST['description'];
    $founded = $_POST['founded'];
    $category = $_POST['category'];
    $fileName = $_FILES['userfile']['name'];
    $tmpName  = $_FILES['userfile']['tmp_name'];
    $fileSize = $_FILES['userfile']['size'];
    $fileType = $_FILES['userfile']['type'];

    $uploaded_dir = "/httpdocs/uploads/";
    $path = $uploaded_dir . $fileName;

    print "Temporary name: " . $_FILES['userfile']['tmp_name'] . "<br>";
    print "Original name: $filename<br>";
    print "Destination: $path<br>";

    if (move_uploaded_file($_FILES["userfile"]["tmp_name"], $path)) {
        print "Uploaded file moved";
        // do something with the file here
    } else {
        print "Move failed";
    }

    $fp      = fopen($tmpName, 'r');
    $content = fread($fp, filesize($tmpName));
    $content = addslashes($content);
    fclose($fp);

    if(!get_magic_quotes_gpc()){
        $fileName = addslashes($fileName);
    }

    // Connects to your Database
    mysql_connect("localhost", "root", "") or die(mysql_error()) ;
    mysql_select_db("my_db") or die(mysql_error()) ;


    //Writes the information to the database
    $query = "INSERT INTO mytable (name, description, founded, category, logo)".
    "VALUES ('$name', '$description', '$founded', '$category', '$fileName')";

    mysql_query($query) or die('Error, query failed');
    include 'library/closedb.php';

    echo "<br>File $fileName uploaded<br>";
}

这是我每次收到的回复

Temporary name: /tmp/phpA8JjRz
Original name:
Destination: /httpdocs/uploads/fixed.png
Move failed
File fixed.png uploaded

这是我的表格

<form method="POST" action="path" enctype="multipart/form-data">
<label>Flying School</label><br />
<input type="text" name="name" id="fsn" placeholder="Flying School Name" required/><br />
<label>Category</label><br />
<select name="category">
  <option value="0">one</option>
  <option value="1">two</option>
  <option value="2">three</option>
  <option value="3">four</option>
</select><br />
<label>Founded</label><br />
<input type="text" name="founded" id="founded" placeholder="yyyy-mm-dd" /><br />
<label>Logo</label><br />
<!--<div class="uploadlogo">-->
<input type="file" name="userfile" />
<!--</div>--><br />
<label>Cover Image</label><br />
<div class="coverimage">
<input type="file" name="cover" id="cover" />
</div><br />
<label>Description</label><br />
<?php
  if (isset($_POST['description'])) $initialentry=$_POST['description'];
  else $initialentry='';
  $editor = JFactory::getEditor();
  echo $editor->display( 'description',  $initialentry, '80%', '350', '55', '20', false  ) ;
?><br />
<label>Photos</label><br />
<ul id="addPhotos">
<li><div class="upload1">
<input type="file" name="acimg1" id="acimg1" />
</div></li>
<li><div class="upload2">
<input type="file" name="acimg2" id="acimg2" />
</div></li>
<li><div class="upload3">
<input type="file" name="acimg3" id="acimg3" />
</div></li>
</ul>
<br />
<label>Choose here</label><br />
<label class="checkbox-inline">
  <input type="checkbox" value="Rss">R22
</label>
<label class="checkbox-inline">
  <input type="checkbox" value="R44">R44
</label>
<label class="checkbox-inline">
  <input type="checkbox" value="R66">R66
</label>
<label class="checkbox-inline">
  <input type="checkbox" value="AS355">AS355
</label>
<label class="checkbox-inline">
  <input type="checkbox" value="PA28">PA28
</label>
<label class="checkbox-inline">
  <input type="checkbox" value="CESSNA172">Cessna 172
</label>
<label class="checkbox-inline">
  <input type="checkbox" value="CESSNA152">Cessna 152
</label><br />
<input class="btn btn-primary" type="submit" name="submit" value="Submit" />

提前致谢!

【问题讨论】:

  • 检查文件夹权限和路径。错误报告将为您提供帮助
  • 另外,我们无法判断您的表格是否正确
  • /httpdocs/uploads/ 应该是完整的系统路径。即:/var/usr/httpdocs/uploads/ 或相对路径,即:../uploads/。我怀疑你的根是/httpdocs/。使用phpinfo() 检查根是什么。
  • @Velnias 这是我遇到的确切问题,但多亏了 Fred,我将路径更改为完整路径,它就像梦一样工作
  • @Fred-ii- 听起来像是对我的回答。

标签: php mysql sql database upload


【解决方案1】:

“已解决!!!谢谢,我找到了文件夹的真实路径,它的工作就像做梦一样!!你我的朋友是救生员!——戴夫林奇”

将我的评论放在答案中:

/httpdocs/uploads/ 应该是完整的系统路径。

即:/var/usr/httpdocs/uploads/ 或相对路径。即:../uploads/

我怀疑你的根是/httpdocs/。在大多数服务器上,系统路径通常以/var/usr/ 开头。其他人有/var/usr/public_html/ 等。还有更多,但这些已经足够了。

使用phpinfo() 会告诉你你的系统路径是什么。

也如 cmets 所述:

您当前的代码对SQL injection 开放。使用prepared statements,或PDOprepared statements

【讨论】:

    【解决方案2】:

    首先:检查管理员权限您选择移动上传文件的路径

    其次:检查 root 文件夹是否存在于目录path/ 中,否则您将无法移动上传的文件。尝试将../dirname返回更改您的dir

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-04
      • 1970-01-01
      • 2013-02-19
      • 1970-01-01
      • 2011-05-13
      相关资源
      最近更新 更多