添加警告消息:“图像已成功上传”

时间:2015-01-08 14:35:48

标签: image upload message

在正确加载图片时,您能告诉我如何添加消息吗?

(如果您对代码有任何建议,我很感激)

<?
function upload()
{
  $result = false;
  $alwidth = 1150;            // maximum allowed width, in pixels
  $alheight = 1150;           // maximum allowed height, in pixels
  $max_size = 300000;          // maximum file size, in KiloBytes
  $allowtype = array('bmp', 'gif', 'jpg', 'jpe', 'png');        // allowed extensions
  $immagine = '';
  $size = 0;
  $type = '';
  $nome = '';

 //check if its allowed or not
 $allowtype = array(".jpg",".jpeg",".gif",".png"); 
 if (!(in_array($file_ext, $allowtype))) {
    die ('<h1 class=\"cover-heading\">Not allowed extension.<br />Please upload images only</h1>');
 }

    $type = $_FILES['file']['type'];
    $nome = $_FILES['file']['name'];
    $immagine = @file_get_contents($_FILES['file']['tmp_name']);
    $immagine = addslashes ($immagine);
    @include 'config.php';
    $sql = "INSERT INTO immagini (nome, size, type, immagine) VALUES ('$nome','$size','$type','$immagine')";
    $result = @mysql_query ($sql) or die (mysql_error());
    return true;

}

?>

1 个答案:

答案 0 :(得分:0)

此代码可帮助您在上传图片时收到消息。有关文件上传的更多信息,请参见here

$target_dir = "uploads/";  /* Target Folder */
$target_file = $target_dir . basename($_FILES["file"]["name"]); /* Your File Name */  
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
/* Checking FILE Type */
if($imageFileType != "jpg" && $imageFileType != "bmp" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
    die ('<h1 class=\"cover-heading\">Not allowed extension.<br />Please upload images only</h1>');    
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {        
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";        
    } else {        
        die ('<h1 class=\"cover-heading\">File Not Uploaded.<br />Please upload images only</h1>');        
    }
}