将图像上传到服务器后获取损坏的URL

时间:2015-08-07 21:59:16

标签: php image url upload directory

<?php
 
// Path to move uploaded files
//$target_path = "uploads/";
 
// array for final json respone
$response = array();
 
// getting server ip address
$server_ip = gethostbyname(gethostname());
 
$bid = isset($_POST['bid']) ? $_POST['bid'] : '';
if (!file_exists($bid)) {
    mkdir("uploads/$bid/", 0700);
}
$target_path = "uploads/$bid";
echo $target_path;

// final file url that is being uploaded
$file_upload_url = 'http://' . $server_ip . '/' . 'AndroidFileUpload' . '/' . $target_path;
 
 
if (isset($_FILES['image']['name'])) {
    $target_path = $target_path . basename($_FILES['image']['name']);
 
    // reading other post parameters
    $email = isset($_POST['email']) ? $_POST['email'] : '';
    $website = isset($_POST['website']) ? $_POST['website'] : '';
 
    $response['file_name'] = basename($_FILES['image']['name']);
    $response['email'] = $email;
    $response['website'] = $website;
 
    try {
        // Throws exception incase file is not being moved
        if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
            // make error flag true
            $response['error'] = true;
            $response['message'] = 'Could not move the file!';
        }
 
        // File successfully uploaded
        $response['message'] = 'File uploaded successfully!';
        $response['error'] = false;
        $response['file_path'] = $file_upload_url . basename($_FILES['image']['name']);
    } catch (Exception $e) {
        // Exception occurred. Make error flag true
        $response['error'] = true;
        $response['message'] = $e->getMessage();
    }
} else {
    // File parameter is missing
    $response['error'] = true;
    $response['message'] = 'Not received any file!F';
}
 
// Echo final json response to client
echo json_encode($response);
?>

我想将每个图片添加到唯一目录?但是当我尝试运行这样的脚本时,我试图打开上传的图像到服务器时得到404。如果我不使用$bid,我会获得工作网址,但没有$bid创建的目录。那么我应该怎样做才能达到效果呢?

0 个答案:

没有答案