我有一個名爲test.php的文件。在這個文件中,我寫了下面的代碼來上傳圖片(.PNG和.JPG)。我還添加了一些代碼,以便在上傳之前預覽圖片... 沒有什麼似乎是錯的,但是當我按SUBMIT按鈕時,什麼也沒有發生...... 爲什麼?我的問題在哪裏?預覽圖片上傳器無法正常工作
更新:我做出改變,現在我得到這樣的警告: 警告:......提供的foreach()無效參數
test.php的:
<script type="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<body>
<?php
if (isset($_POST[ 'submit' ])) {
define ("UPLOAD_DIR" , "uploaded/pic/");
foreach ($_FILES["images"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$name = $_FILES["images"]["name"][$key];
$info = getimagesize($_FILES["images"]["tmp_name"][$key]);
$image_type = $info[2];
$type = $_FILES['images']['type'][$key];
// if the image is .JPG or .PNG
if (($image_type == 3) || ($image_type == 2)){
// ensure a safe filename
$name = preg_replace("/[^A-Z0-9._-]/i", "_", $name);
// don't overwrite an existing file
$i = 0;
$parts = pathinfo($name);
while (file_exists(UPLOAD_DIR . $name)) {
$i++;
$name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
}
// preserve file from temporary directory
$success = move_uploaded_file($_FILES["images"]["tmp_name"][$key], UPLOAD_DIR . $name);
if (!$success) {
echo "<p>Unable to save file.</p>";
exit;
}
// set proper permissions on the new file
chmod(UPLOAD_DIR . $name, 0644);
echo "<h2>Successfully Uploaded Images</h2>";
}
else{
echo "<h2>format not supported... </h2>";
}
}
}
}
?>
<div id="upload_form">
<form id="frm1" name="frm1" method="post" action="test.php" enctype="multipart/form-data">
<p>
<label for="images">insert your image</label>
<input type="file" name="images" id="images" tabindex="80"/>
</p>
<img id="pic" name="pic" src="#" />
<button type="submit" id="submit" name="submit">Upload Files!</button>
</form>
<script type="text/javascript" language="javascript">
// Preview the picture before Uploading on the server!
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#pic').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#images").change(function(){
readURL(this);
});
</script>
</div>
我在某些情況下見過''不起作用。嘗試使用''反而看看是否有效。如果不是,那麼問題顯然在別處。 –
試試這個「命名」版本''或''很確定這會起作用,因爲使用你的'if(isset($ _ POST ['submit']))'正在尋找一個名爲「**」的按鈕,而你沒有被命名。 –
@ Fred-ii - 我試過了和上傳文件!但現在得到這個錯誤:警告:爲foreach()提供的無效參數... –