[英]Image upload failing in PHP
所以我試圖通過本地主機上的php通過表單設置圖像上傳,並在運行代碼並連接到數據庫之后,我收到了上傳錯誤。 由於表單的所有其他部分在逐節檢查后都可以正常工作,因此我只添加用於上傳輸入的html及其相關的php腳本。
<input type="file" name="image" id="image-select" />
php中必須處理圖片上傳和上傳后驗證的部分:
$image = $_FILES ['image']['name'];
$type = @getimagesize ($_FILES ['image']['tmp_name']);
//Never assume image will upload okay
if ($_FILES['image']['error'] !== UPLOAD_ERR_OK) {
die("Upload failed with error code " . $_FILES['image']['error']);
}
//Where the file will be placed
$target_path = "uploads/";
// Add the original filename to target path
$path= $target_path . basename($image);
// Check if the image is invalid before continuing
if($type === FALSE || !($type[2] === IMAGETYPE_TIFF || $type[2] === IMAGETYPE_JPEG || $type[2] === IMAGETYPE_PNG)) {
echo '<script "text/javascript">alert("This is not a valid image file!")</script>';
die("This is not a valid image file!");
} else {
move_uploaded_file($_FILES['image']['tmp_name'], $path);
}
因此,一旦代碼到達上傳過程,就會出現錯誤。 我試圖上載一個小的PNG文件。當它們出現在腳本中時,添加的相關代碼也按其各自的順序排列。
此處只是一片黑暗,因為您沒有提供表單標簽,但您還記得enctype屬性嗎?
抱歉,我沒有50信譽,否則我會以此作為評論。
<form enctype='multipart/form-data' action=''>
<input type="file" name="image" id="image-select" />
</form>
另外,您還應該在調用getimagesize()之前檢查文件是否已上傳成功(不是這會成為問題的根源)
如果您使用的是PHP5和Imagick ,那么您可以嘗試使用類似以下的內容:
$image->readImageFile($f);
你能在這里輸入錯誤嗎? 如果錯誤表示提交頁面文件后未上傳。 然后請檢查您的表單密碼。 參見以下示例
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="image" id="image-select" />
<input type="submit" value="Submit">
</form>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.