我一直在制作一个预制的图片上传模板以获得乐趣,而且我对php不太好,所以我似乎无法解决这个问题。
我需要能够提取到我的图片的直接链接($upload_dir
变量+文件扩展名),以便我可以在我的index.php
页面中使用它。我尝试将post_file.php
文件包含在我的index.php
中,但我收到了Wrong HTTP-method
消息。
我的网站位于html5
,并且正在使用拖放功能启动上传,如果这与我的上传文件混淆。
post_file.php :
$demo_mode = false;
$upload_dir = 'uploads/'.date(d).rand(0000,9999);
$allowed_ext = array('jpg','jpeg','png','gif');
if (strtolower($_SERVER['REQUEST_METHOD']) != 'post') {
exit_status('Error! Wrong HTTP method!');
}
if (array_key_exists('pic',$_FILES) && $_FILES['pic']['error'] == 0 ) {
$pic = $_FILES['pic'];
if (!in_array(get_extension($pic['name']),$allowed_ext)) {
exit_status('Only '.implode(',',$allowed_ext).' files are allowed!');
}
// Uploads are being ignored, but logged.
if ($demo_mode) {
$line = implode(' ', array( date('r'), $_SERVER['REMOTE_ADDR'], $pic['size'], $pic['name']));
file_put_contents('log.txt', $line.PHP_EOL, FILE_APPEND);
exit_status('Uploads are ignored in demo mode.');
}
// Move image,rename it and log it all
if (move_uploaded_file($pic['tmp_name'], $upload_dir.'.'.get_extension($pic['name']))) {
$line = implode(' ', array( date(r), 'http://website.com/'.$upload_dir.'.'.get_extension($pic['name'])));
file_put_contents('log.txt', $line.PHP_EOL, FILE_APPEND);
exit_status('Upload was successful');
}
}
exit_status('Something went wrong with your upload!');
功能
function exit_status($str) {
echo json_encode(array('status'=>$str));
exit;
}
function get_extension($file_name) {
$ext = explode('.', $file_name);
$ext = array_pop($ext);
return strtolower($ext);
}
当我在收到错误消息时使用include('post_file.php')
时,如何在我的post_file.php
文件中不包含index.php
的情况下获取新的图片名称+路径和扩展名在变量中回显它?
提前致谢!