PHP 4.0 講義 PHP介紹 1 2 3 4 5 6 7 8 9 A B C D 


指令敘述 8.1 :  檔案上傳

指令說明:利用HTML標籤的 type="file" 來上傳檔案

例:

( 注意權限的問題 , 先用winscp建立一個資料夾 temp, 再該資料夾上按右鍵, 選 properties, 把w屬性打勾 )
檔名為 sendfile1.php

<form name="form1" method="post" enctype="multipart/form-data" action="sendfile2.php">
<p>Sender:<input type="text" name="uname"></p>
<p>Select a file...</p>
<p><input type="file" name="ufile"></p>
<p>
<input type="submit" name="Submit" value="Send">
<input name="Reset" type="reset" id="Reset" value="Reset">
</p>
</form>

檔名為 sendfile2.php

<?php
$uname=$_POST["uname"];
$error_msg=$_FILES["ufile"]["error"];
$fname=$_FILES["ufile"]["name"];
$tmpname=$_FILES["ufile"]["tmp_name"];
$fsize=$_FILES["ufile"]["size"];
$ftype=$_FILES["ufile"]["type"];
echo "Hello, $uname <br>";
if($error_msg)
{
echo "<font color=red>Upload failed !</font><p>";
echo "Error code : $error_msg ";
}
else
{
// upload successful
echo "Upload file name is $fname <br>";
echo "Upload file size is $fsize <br>";
echo "Upload file type is $ftype <br>";
echo "<hr>";

// save this file to a temp directory
$success=copy($tmpname,"temp/".$fname);

if($success)
{
echo "Save file is successful !<br>";
echo "Save as : temp/$fname<br>";

unlink($tmpname);
}
else
{
echo "<font color=red> Error to save this file! </font>";
}
}
?>

 


PHP 4.0 講義 PHP介紹 1 2 3 4 5 6 7 8 9 A B C D