【问题标题】:move_uploaded_file not writing to server, nor returning anythingmove_uploaded_file 不写入服务器,不返回任何内容
【发布时间】:2015-07-24 22:14:59
【问题描述】:

我正在尝试将一个文件以及一些文本字段从 HTML 表单存储到我的 Web 服务器。文字写得很好,但是图像没有任何反应。我检查了错误,但没有出现。该图像也在电子邮件中收到。 我相信权限也很好 - 它们在 /img/gallery//img/gallery/data/ 上完全相同。

这是我的 PHP 表单:

<?php
include 'loadImages.php';
require_once('class.phpmailer.php');

$name = $_POST['name'];
$email = $_POST['email'];
$location = $_POST['location'];
$desc = $_POST['desc'];

/**
 * PHPMailer simple file upload and send example
 */
$msg = '';
if (array_key_exists('userfile', $_FILES)) {
    // First handle the upload
    // Don't trust provided filename - same goes for MIME types
    // See http://php.net/manual/en/features.file-upload.php#114004 for more thorough upload validation
    $uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['userfile']['name']));
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        // Upload handled successfully
        // Now create a message
        // This should be somewhere in your include_path
        require 'PHPMailerAutoload.php';
        $mail = new PHPMailer;
        $mail->setFrom('gallery@####.co.uk', 'Website');
        $mail->addAddress('gallery@####.co.uk');
        $mail->Subject = 'Image Submission Recieved';
        $mail->msgHTML("<b>Name</b>: $name<br><b>Email</b>: $email<br><b>Location</b>: $location<br><b>Description</b>: $desc");
        // Attach the uploaded file
        $mail->addAttachment($uploadfile, 'Image');
        if (!$mail->send()) {
            $msg = "Mailer Error: " . $mail->ErrorInfo;
        } else {
            $msg = "Your message has been sent.";
        }
    } else {
        $msg = 'Failed to move file to ' . $uploadfile;
    }

    $date = date('Y-m-d_H-i-s');

    $path = $_SERVER['DOCUMENT_ROOT'].'/img/gallery/data/';
    $filename = $date.".txt";

    $file = fopen($path.$filename, "w") or die("Something went wrong storing your photo information");
      fwrite($file, $name . "\n");
      fwrite($file, $location . "\n");
      fwrite($file, $desc . "\n");
      fwrite($file, $email . "\n");
      fwrite($file, "Valid: False");
    fclose($file);

    $name = $_FILES['userfile']['name'];
    $path = $_SERVER['DOCUMENT_ROOT'].'/img/gallery/';
    $filename = $date . "." . pathinfo($name, PATHINFO_EXTENSION);

    $target = $path . $filename;
    echo $target;

    echo move_uploaded_file( $_FILES['userfile']['tmp_name'], $target);
}

$images = loadImages();
$imageData = LoadImageData();
?>

$_FILES:

1Array ( [userfile] => Array ( [name] => hairy sun.jpg [type] => image/jpeg [tmp_name] => /tmp/phpWweCkX [error] => 0 [size] => 49526 ) )

如果你还需要看什么,我会上传。

【问题讨论】:

  • 您在 error_log 中看到了哪些错误?
  • 你能发布 print_r($_FILES) 的输出吗?
  • @skrilled 我没有收到任何错误。
  • @Jaime 立即更新。

标签: php html forms file-upload fwrite


【解决方案1】:

转移这个,

$date = date('Y-m-d_H-i-s');

$path = $_SERVER['DOCUMENT_ROOT'].'/img/gallery/data/';
$filename = $date.".txt";

$file = fopen($path.$filename, "w") or die("Something went wrong storing your photo information");
  fwrite($file, $name . "\n");
  fwrite($file, $location . "\n");
  fwrite($file, $desc . "\n");
  fwrite($file, $email . "\n");
  fwrite($file, "Valid: False");
fclose($file);

$name = $_FILES['userfile']['name'];
$path = $_SERVER['DOCUMENT_ROOT'].'/img/gallery/';
$filename = $date . "." . pathinfo($name, PATHINFO_EXTENSION);

$target = $path . $filename;
echo $target;

echo move_uploaded_file( $_FILES['userfile']['tmp_name'], $target);

以上代码,

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {

然后再试一次。

【讨论】:

  • 我现在收到failed to move file to /temp/74ed5209e101d58611901d2b7d518cc11622a08e5HfbbE
  • 图片实际上正在上传,尽管出现了这个错误。
  • 我相信这是因为我上传了两次。傻我。
【解决方案2】:

您将它保存到 /tmp 目录,我认为脚本退出时它会被清理。尝试保存到另一个目录。

【讨论】:

  • 我将第 18 行更改为 $uploadfile = tempnam($_SERVER['DOCUMENT_ROOT'].'/img/gallery/temp/', sha1($_FILES['userfile']['name']));,但结果仍然相同。这是你的意思吗?
猜你喜欢
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-15
  • 1970-01-01
  • 2012-04-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多