2010-12-07 12 views
0

Der CodePHP --- Warum kann ich kein Bild mit PHP hochladen?

<?php 
    $allowed_filetypes = array('.jpg','.gif','.bmp','.png'); 
    $max_filesize = 5242888; 
    $upload_path = '/files'; 
    $filename =$_FILES['userfile']['name']; 
    $ext = substr($filename,strpos($filename,'.'),strlen($filename)-1); //Get the  extension form the filename. 

    if(!in_array($ext,$allowed_filetypes)) 
     die('the file you attempted to upload is not allowed.'); 

    if(filesize($_FILES['userfile']['size'])>$max_filesize) 
     die('the file you attempted to upload is too large.'); 

    if(!is_writable($upload_path)) { 
     die('you cannot upload to the specified directory,please CHMOD it to 777.'); 
    } 

    if (move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path.$filename)) 
    { 
     echo 'you file upload successful.view the file <a  href=".$upload_path.$filename.title="your file">here</a>'; 
    } 
    else{ 
     echo 'failed'; 
    } 

Wenn ich ein JPEG-Bild hochladen, es zeigt die Fehlermeldung „Die Datei, die Sie zu laden versucht wird, nicht erlaubt.“. Was ist falsch an meinem Code?

Der Haupt HTML-Code:

<form action="upload.php" method="post" enctype="multipart/form-data"> 
    <input type="file" name="userfile" id="file"/> 
    <button>upload</button> 

Antwort

0

Versuchen Sie ersetzen:

$ext = substr($filename,strpos($filename,'.'),strlen($filename)-1); 
//get the extension form the filename 

von

$ext = substr($filename,strpos($filename,'.'),4); 
//get the  extension form the filename 
+0

Sie sollten $ ext durch Drucken überprüfen, so dass wir sicher sind, dass es in $ allowed_filetypes –

+0

ich Strlen ($ Dateiname) -1 bis 3 geändert, aber es zeigt immer noch den Fehler. – runeveryday

+0

Sie haben Recht, wenn ich die $ ext-Datei abbilde, zeigt es an, dass die Datei, die Sie hochladen wollten, nicht erlaubt ist. Was ist falsch an meinem Code? danke – runeveryday

2

Bitte verwenden Sie in Formular-Tag

<form action="upload.php" method="post" enctype="multipart/form-data"> 
+0

Ich ändere das Formular tag.aber es funktioniert immer noch nicht. – runeveryday

+0

Dann fügen Sie eine Bilderweiterung nicht im Array hinzu und ändern dann das Array als Array ('. Jpg', '. Gif', '. Bmp', '. Png', 'JPG', '. GIF', '. BMP', "PNG"); und echo $ ext varriable und check –

0

Versuchen Sie es mit einer Senden-Schaltfläche, < input type = "submit" name = "upload" value = "upload" />.

0
<form action="upload.php" method="post" enctype="multipart/form-data"> 
    <input type="file" name="userfile" id="file"/> 
    <input type="submit" name="sendFile" value="Upload" /> 
</form> 

das sollte funktionieren.