2012-02-16 17 views
1

これは、Apache htdocsのフォルダに写真をアップロードするWebページを作成しようとしています。そして...うーん...うまくいきません...私はこれに関するチュートリアルをどこからでも探していて、PHP全体で処理する必要があります(一部のHTMLでも[Flashなし])。ここでPHP(Apache内の)フォルダにファイルをアップロードする

は私が持っているものです....

<p>Browse For a File on your computer to upload it!</p> 
<form enctype="multipart/form-data" action="upload_photos.php" method="POST"> 
<input type="hidden" name="MAX_FILE_SIZE" value="250000" /> 
Choose a file to upload: <input name="uploadedfile" type="file" /><br /> 
<input type="submit" value="Upload File" /> 
</form> 


     <?PHP  

    if ($uploadedfile_size >250000) 
    {$msg=$msg."Your uploaded file size is more than 250KB so please reduce the file size and then upload.<BR>"; 
    $file_upload="false";} 

    else{ 

    if (!($uploadedfile_type<>"image/jpeg" OR $userfile_type<>"image/tiff" OR $userfile_type<>"image/png")) 
    {$msg=$msg."Your uploaded file must be of JPG, PNG, or tiff. Other file types are not allowed<BR>"; 
    $file_upload="false";} 

    } 
     ?> 

     <!-- 
    •enctype="multipart/form-data" - Necessary for our PHP file to function properly. 
    •action="upload_photos.php" - The name of our PHP page that was created. 
    •method="POST" - Informs the browser that we want to send information to the server using POST. 
    •input type="hidden" name="MA... - Sets the maximum allowable file size, in bytes, that can be uploaded. This safety mechanism is easily bypassed and we will show a solid backup solution in PHP. We have set the max file size to 100KB in this example. 
    •input name="uploadedfile" - uploadedfile is how we will access the file in our PHP script. 

     --> 

</form> 

    </label> 
</form> 

は、今ではこの点を過ぎて、しかし、それは他のページ(upload_photos.php)に到達する到達したら、それは、「ファイルのアップロードエラーが発生しましたと言います、もう一度お試しください!

<?php 

/* 
When the uploader.php file is executed, the uploaded file exists in a temporary storage area on the server. If the file is not moved to a different location it will be destroyed! To save our precious file we are going to need to make use of the $_FILES associative array. 

The $_FILES array is where PHP stores all the information about files. There are two elements of this array that we will need to understand for this example. 
•uploadedfile - uploadedfile is the reference we assigned in our HTML form. We will need this to tell the $_FILES array which file we want to play around with. 
•$_FILES['uploadedfile']['name'] - name contains the original path of the user uploaded file. 
•$_FILES['uploadedfile']['tmp_name'] - tmp_name contains the path to the temporary file that resides on the server. The file should exist on the server in a temporary directory with a temporary name. 

*/ 

// Where the file is going to be placed 
$target_path = "uploads/"; 

/* Add the original filename to our target path. 
Result is "uploads/filename.extension" */ 
$target_path = $target_path . basename($_FILES['uploadedfile']['name']); 


/* 
Now all we have to do is call the move_uploaded_file function and let PHP do its magic. The move_uploaded_file function needs to know 1) The path of the temporary file (check!) 2) The path where it is to be moved to (check!). 
*/ 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
    echo "The file ". basename($_FILES['uploadedfile']['name']). 
    " has been uploaded"; 
} else{ 
    echo "There was an error uploading the file, please try again!"; 
} 


/* 

If the upload is successful, then you will see the text "The file filename has been uploaded". This is because move_uploaded_file returns true if the file was moved, and false if it had a problem. 

If there was a problem then the error message "There was an error uploading the file, please try again!" would be displayed. 

*/ 

?> 

が私を助けてください、私は行ずつそれを見ていると、それはそれをやっている理由を私は知りません。おそらく構文エラーなのかもしれません。私はPHPにまったく新しいので、私はそれを疑うことはありません。とにかく、前もって感謝します。

+0

$ target_pathを完全なサーバーパスに変更します。 –

+0

関連するApacheの設定は何ですか? Apacheは、ファイルアップロードのタイプとサイズに制限を課すこともできます。そしてあなたのApacheエラーログは何を言っていますか? –

+0

"私はPHPの初心者です" - **あなたの**コードをデバッグする方法を学ぶ絶好の機会です。誰もあなたのためにそれをすることはありません、それはプログラマーとしてあなたの毎日の仕事です – zerkms

答えて

0

ドロップPHPinfo()ファイルを返し、ApacheはCORE下upload_max_filesizeでのために持っているかどうか確認します。これは低く設定されている可能性があります。

あなたが見落とした場合は、対象のディレクトリの権限を確認してください。ターゲットディレクトリが775に設定されていること、または書き込み先のディレクトリがchmodであることを確認してください。

0

変更:

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
    echo "The file ". basename($_FILES['uploadedfile']['name']). 
    " has been uploaded"; 
} else{ 
    echo "There was an error uploading the file, please try again!"; 
} 

へ:

はその後、私たちはそののvar_dumpをお知らせダウン

+0

少しでもやります。今、私のラップトップは現在(もちろん)アップデートをしています。 (私はずっと前にラップトップを手に入れていたので、多くのWindowsアップデートがあります) –

+0

大丈夫、var_dumpが返す唯一の事は "bool(false)"です。 $ target_pathをフルパスに変更しようとしました... chmodのアクセス権も試してみます –

関連する問題