我这里有一个处理图像上传的代码。
但由于某种原因,$ file_destination变量无法识别,超出了ifs语句。基本上我想要达到的是在标签内输出$ file_destination变量。但是,当我使用变量时,它不会在外面工作。
//FILE UPLOADING CODES (NEED TO BE INSIDE A FUNCTION)
global $file_destination;
if(isset($_FILES['file'])){
$file = $_FILES['file'];
print_r($file);
// File properties
$file_name = $file['name'];
$file_tmp = $file['tmp_name'];
$file_size = $file['size'];
$file_error = $file['error'];
//File Extension
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));
//Check which file type allowed
$allowed = array('png', 'jpg');
if(in_array($file_ext, $allowed)){
if($file_error === 0){
if($file_size <= 2097152){
$file_name_new = uniqid('', true). '.'.$file_ext;
$file_destination = 'C:/'.$file_name_new;
if(move_uploaded_file($file_tmp, $file_destination)){
echo $file_destination;
}
}
}
}
}
echo <img src="<?php $file_destination ?>">
由于$ file_destination无法识别,因此无法正常工作。任何的想法?
答案 0 :(得分:0)
也许您的问题是浏览器的安全性。有些浏览器不允许你从你的HD显示本地文件:c:/myimages.jpg
尝试指出你的&#34; src&#34;到Web服务器中的资源(图像文件):
实施例:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JS TDD</title>
<link rel="stylesheet" type="text/css" href="style.css">
<!-- import mocha.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.4.5/mocha.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.4.5/mocha.min.css"></script>
<!-- customization mocha.js -->
<script>mocha.setup('bdd');</script>
<!-- import chai.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.5.0/chai.js"></script>
<!-- customization chai.js (optional) -->
<script>
var assert = chai.assert;
</script>
</head>
<body>
<h1 class="title">Learn unit test!</h1>
<!-- script for test -->
<script>
function pow() {
return 8; // I am lier!
}
</script>
<!-- upload custom test -->
<script src="test.js"></script>
<!-- result of custom test -->
<div id="mocha"></div>
<!-- run mocha! -->
<script>mocha.run();</script>
</body>
</html>