[英]Why does my Php Image upload not work?
我嘗試讓用戶上傳圖片,但是它不起作用。 沒有錯誤或任何內容,但文件沒有出現在文件夾中。
$id = $rowcount + 1;
$pfad_imgfront = "bilder/front/";
$pfad_imgback = "bilder/back/";
//Fileupload
if ($_FILES['imgfront']['size'] > 0) {
$pfad_imgfront = $pfad_imgfront . basename($_FILES['imgfront']['name']);
$dateityp = pathinfo($pfad_imgfront, PATHINFO_EXTENSION);
if ($_FILES["imgfront"]["size"] > 2000000) {
echo "Ihr Bild ist grösser als 2MB.";
$uploadOk = 0;
}
if ($dateityp != "jpg" && $dateityp != "png" && $dateityp != "jpeg" && $dateityp != "gif" && $dateityp != "bmp") {
echo "Nur JPG, JPEG, PNG, BMP & GIF Dateien sind erlaubt.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["imgfront"]["tmp_name"], $pfad_imgfront)) {
rename($pfad_imgfront, $id . 'front');
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
用戶請求的html表單:
<form enctype="multipart/form-data" action="neu_aufgabe.php"
method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
Bild zur Aufgabe (max. 2MB): <input name="imgfront" type="file" /><br />
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
Bild zur Lösung (max. 2MB): <input name="imgback" type="file" /><br />
<input type="submit" value="Senden" /> </form>
歡迎任何幫助:D
希望以下內容可以幫助您正常工作-可以在我的測試系統上很好地上傳兩個文件
<?php
$id=$rowcount+1;
$root = realpath( $_SERVER['DOCUMENT_ROOT'] );
foreach( $_FILES as $field => $arr ){
$errors=array();
$size=$_FILES[ $field ]['size'];
if( $size > 0 ){
$filename = $_FILES[ $field ]['name'][0];
switch( $field ){
case 'imgfront': $path=realpath( $root . '/bilder/front/' ); $newname=$path . DIRECTORY_SEPARATOR . $id . 'front' . $filename; break;
case 'imgback': $path=realpath( $root . '/bilder/back/' ); $newname=$path . DIRECTORY_SEPARATOR . $id . 'back' . $filename; break;
}
$imgpath = $path . DIRECTORY_SEPARATOR . $filename;
$ext = strtolower( pathinfo( $imgpath, PATHINFO_EXTENSION ) );
if( isset( $_POST['MAX_FILE_SIZE'] ) && $size > $_POST['MAX_FILE_SIZE'] ){
$errors[]='File too large';
}
if( !in_array( $ext, array('jpg','png','gif','bmp') ) ){
$errors[]='File is wrong type';
}
if( !empty( $errors ) ){
print_r( $errors );
} else {
$res=move_uploaded_file( $_FILES[ $field ]["tmp_name"][0], $imgpath );
if( $res ) rename( $imgpath, $newname );
}
}
}
?>
<form enctype="multipart/form-data" action="/test/target.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<div>
Bild zur Aufgabe (max. 2MB): <input name="imgfront[]" id='fr' type="file" />
</div>
<div>
Bild zur Lösung (max. 2MB): <input name="imgback[]" id='bk' type="file" />
</div>
<input type="submit" value="Senden" />
</form>
看起來您從來沒有將$ uploadOk設置為0以外的任何其他值。在執行if($ uploadOk == 0)檢查之前,需要將其設置為其他值。 我建議在您的第一個if語句之外的頂部附近添加此代碼:
$uploadOk = 1;
使用服務器上的絕對路徑保存圖像
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
$pfad_imgfront = $root ."/bilder/front/";
$pfad_imgback = $root."/bilder/back/";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.