dongshou1991 2013-05-18 16:31
浏览 37
已采纳

功能不会移动图像,但会正确返回信息

I'm tryin to upload 2 images but it seems all to be well but it move_uploaded_file() doesn't move the file, no errors have been generated.

// Upload Image
function upload_image($tmp,$name) {
    $ext = explode('.',$name);
    if (preg_match('~(jpeg|jpg|gif|png)$~i', $name)) {
    $ext = end($ext);

    $name = substr(md5(rand()), 0, 10).'.' . $ext;
    $upload_dir = '/music/uploads/' . $name;
    echo $tmp;
    move_uploaded_file($tmp,'/home/shadne/public_html'.$upload_dir);

        return $upload_dir;
    } else {
        throw new ErrorException('File type not allowed');
    }
}

file/folder permission is 0777, i used images/new before using /uploads

EDIT 1

code that handles the uploading and checkin

<?php
require_once '../config.php';
require 'includes/functions.php';

if (!empty($_FILES['artist_profile_image'])) {
    $profile_image_name = $_FILES['artist_profile_image']['name'];
    $profile_image_tmp = $_FILES['artist_profile_image']['tmp'];
    $profile_image_dir = upload_image($profile_image_tmp, $profile_image_name);
}

if (!empty($_FILES['artist_thumb'])) {
    $thumb_name = $_FILES['artist_thumb']['name'];
    $thumb_tmp = $_FILES['artist_thumb']['tmp'];
    $thumb_dir = upload_image($thumb_tmp, $thumb_name);
}

if (empty($_POST) === false) {
    $required_fields = array('artist_name', 'artist_thumb', 'artist_profile_image', 'birthday');
    foreach ($_POST as $key => $value) {
            if (empty($value) && in_array($key, $required_fields) === true) {
                $errors[] = 'Fields marked with an asterstrik are required';
                break 1;
            }
    }
}

if (isset($_GET['success'])) {
    echo 'You\'ve added a new artist.';
} else {
    if (empty($_POST) === false && empty($errors) === true) {
        $artist_data = array(
            'artist_name'           => $_POST['artist_name'],
            'artist_thumb'          => $thumb_dir,
            'artist_profile_image'  =>  $profile_image_dir,
            'birthday'              => $_POST['birthday'],
        );

        add_artist($artist_data);
        //exit(header('Location: add_artist.php?success'));

    } else if (empty($errors) === false) {
        echo output_errors($errors);
    }
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Admin Area</title>
    </head>
    <body>

<h1>Add Artist</h1>

<form action="" method="post" enctype="multipart/form-data">
    <ul>
        <li>
            Artist Name*: <input type="text" name="artist_name" />
        </li>
        <li>
            Artist Thumbnail*: <input type="file" name="artist_thumb" />
        </li>
        <li>
            Artist Profile Image*: <input type="file" name="artist_profile_image" />
        </li>
        <li>
            Birthday*:
                    <input type="date" name="birthday" />
        </li>                    
        <li>
        <input type="submit" value="Add Artist" />
        </li>
    </ul>
</form>
<?php  } ?>
    </body>
</html>
  • 写回答

1条回答 默认 最新

  • donglue8180 2013-05-18 16:38
    关注

    You need to check if move_uploaded_file returns true or false. It can silently fail if the input file is not a valid upload file. What are you sending in to $tmp?

    Check out: http://php.net/manual/en/function.move-uploaded-file.php for return values.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路