簡體   English   中英

以注冊表格的形式在PHP中進行圖像上傳驗證

[英]Image upload validation in PHP in registration form

我需要檢查圖像是否已上傳的幫助,如果尚未上傳,請將圖像路徑設置為默認圖像路徑(Nopic.png)。 我正在使用表單注冊用戶,並且需要為他們設置個人資料圖像。 表單正常,圖像上傳正常(使用w3c的代碼)。 但是,如何檢查文件字段是否為空,並相應地設置路徑?

上傳后,您可以檢查文件是否在預期目錄中,如果不存在,則使用默認映像:

$image = "../uploads/foo/image.png"; //path to your uploaded image
if(file_exists($image) && (is_file($image))){
    //if exists show the image
}else{
    $image = "../uploads/foo/default.png"; //path to your default image
    //you can then show this image
}

使用is_uploaded_file()

if(!file_exists($_FILES['myfile']['tmp_name']) || !is_uploaded_file($_FILES['myfile']['tmp_name'])) {
echo 'No upload';
}

這是PHP文檔

http://php.net/manual/zh/function.is-uploaded-file.php

檢查文件是否存在:

    if (file_exists ($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name']) ) {
        //yes it exist
    }else{
       //default one (Nopic.png)
   }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM