2015-01-08 43 views
0

您能否告诉我如何在图片正确加载时添加消息?添加一条警告消息:“图片上传成功”

(如果您对代码的建议我很感谢)

<? 
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; 

} 

?> 

回答

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>');   
    } 
}