2015-07-11 101 views
0

我想通過我的PHP腳本(大小約89kb)上傳圖像。我的index.php的代碼是:上傳文件時使用PHP(使用Linode服務器)

<html> 
<head> 
<title>File Uploading Form</title> 
</head> 
<body> 
<h3>File Upload:</h3> 
Select a file to upload: <br /> 
<form action="file_uploader.php" method="post" 
         enctype="multipart/form-data"> 
<input type="file" name="file" size="100" /> 
<br /> 
<input type="submit" value="Upload File" /> 
</form> 
</body> 
</html> 

這是擺在同一個目錄中file_uploader.php

,然後file_uploader.php代碼:

<?php 
if($_FILES['file']['name'] != "") 
{ 
    copy($_FILES['file']['name'], "") or 
      //Exit and show the error message, die does this 
      die("Could not copy file!"); 
} 
else 
{ 
    die("No file specified!"); 
} 
?> 
<html> 
<head> 
<title>Uploading Complete</title> 
</head> 
<body> 
<h2>Uploaded File Info:</h2> 
<ul> 
<li>Sent file: <?php echo $_FILES['file']['name']; ?> 
<li>File size: <?php echo $_FILES['file']['size']; ?> bytes 
<li>File type: <?php echo $_FILES['file']['type']; ?> 
</ul> 
</body> 
</html> 

的權限的目錄索引和file_uploader在:755。

該腳本似乎加載,但我得到錯誤「無法複製文件」,不知道這裏發生了什麼。有人能給我一些指點嗎?謝謝!!

+1

你能發佈整個原始錯誤嗎? –

回答

1

看看documentation然後請解釋一下copy($_FILES['file']['name'], "")應該做什麼?如果我是你,我會修復這個聲明,並用專門爲此創建的更安全的變體替換它:move_uploaded_file

此外,通過this section in the manual閱讀,你可能會發現你的代碼中的另一個錯誤;如果你使用$_FILES[0]['tmp_name']可能會使它工作得更好。

把所有這些放在一起;我會用:

move_uploaded_file($_FILES[0]['tmp_name'], __DIR__ .'/'. $_FILES[0]['name']); 
+0

copy($ _FILES ['file'] ['name'],「」)應該將通過文章提交的文件保存到服務器(與index.php位於同一位置)。 – KexAri

+0

嗯,它不這樣做,對吧?我會爲你添加複製/粘貼代碼 – Sjon

+0

@KexAri爲我做了我的建議嗎?請回復或標記爲 – Sjon

相關問題