ds78662302 2015-07-24 22:14
浏览 118
已采纳

move_uploaded_file没有写入服务器,没有返回任何内容

I am trying to store a file, along with a few text fields, from an HTML form to my web server. The text is being written absolutely fine, however nothing is happening with the image. I have checked for errors, and none are showing up. The image is also being received in the email. I believe the permissions are fine too - they are exactly the same on /img/gallery/ and /img/gallery/data/.

Here is my PHP for the form:

<?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 . "
");
      fwrite($file, $location . "
");
      fwrite($file, $desc . "
");
      fwrite($file, $email . "
");
      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 ) )

If you need to see anything else, I will upload it.

  • 写回答

2条回答 默认 最新

  • droos02800 2015-07-24 22:24
    关注

    shift this,

    $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 . "
    ");
      fwrite($file, $location . "
    ");
      fwrite($file, $desc . "
    ");
      fwrite($file, $email . "
    ");
      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);
    

    above this code,

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

    Then try again.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)