php中的file_upload,PHP文件上传(PHP file upload)
PHP文件上傳(PHP file upload)
我正在嘗試使用php將文件上傳到我的服務器,將它們保存到我的mysql數據庫中,但我無法讓它工作,這是我正在使用的腳本,我相信它與“$ _FILES有關“因為當我把它拿出來時”&& $ _FILES ['userfile'] ['size']> 0“腳本開始運行,但是那個使用”$ _FILES“的變量沒有定義。
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) {
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
db_connect();
db_select();
$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
mysql_query($query) or die('Error, query failed');
db_disconnect();
echo "
File $fileName uploaded
";
}
I am trying to upload files to my server using php to save them into a binary form into my mysql database but I cant get it to work, here is the script I’m using, I believe it has something to do with "$_FILES" because when I take this out "&& $_FILES['userfile']['size'] > 0" the script starts to run but then the variables underneath that use "$_FILES" aren’t defined.
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) {
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
db_connect();
db_select();
$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
mysql_query($query) or die('Error, query failed');
db_disconnect();
echo "
File $fileName uploaded
";
}
原文:https://stackoverflow.com/questions/578054
更新時間:2019-12-08 22:24
最滿意答案
這是一個2折的過程,首先是上傳本身然后是服務器上的操作。 第一個驗證是確保文件甚至上傳。 為此你可以在線后使用
$fileName = $_FILES['userfile']['name'];
使用:
if(is_uploaded_file($fileName)) {
// Here goes all the file manipulation/storage/etc
} else {
echo 'Error: File could not be uploaded.';
}
嘗試使用它,如果文件實際上傳,可能會重新發布。 僅僅因為$ _FILES有內容,并不一定意味著文件已上傳到服務器。
This is a 2 fold process, first the upload itself and then the manipulation on the server. The first validation would be to make sure the file was even uploaded. For that you can use after the line
$fileName = $_FILES['userfile']['name'];
Use:
if(is_uploaded_file($fileName)) {
// Here goes all the file manipulation/storage/etc
} else {
echo 'Error: File could not be uploaded.';
}
Try to use that and maybe re-post if the file was actually uploaded. Just because $_FILES has content it does not necessarily mean that the file was uploaded to the server.
2009-02-23
相關問答
首先,檢查phpinfo()以查看運行時設置是什么; 如果他們仍然顯示2M (默認),Apache可能不遵守.htaccess文件 通常我會告訴您確保在Apache配置的虛擬主機聲明中有這個: AllowOverride Options FileInfo
但是在共享主機上并沒有給你這些權限(極不可能)。 您應該與您的托管服務提供商聯系,看看他們是否可以通過在虛擬主機中添加php_value設置來為您提高這些限制 或者,按照@Satya的建議,通過分塊上傳文件(例如一次上傳1MB)解決問題。 Fi
...
您不能在文件名中使用管道(|)。 這是您遇到錯誤的最可能原因。 嘗試將其更改為下劃線或短劃線。 $rand_img = uniqid();
$file_upload_folder = "uploads";
$finalImgLink = $file_upload_folder . '/' . $rand_img . '_' . $_FILES['file']['name'];
//move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/$r
...
看一下Zip擴展: http://www.php.net/manual/en/zip.examples.php 我看了你鏈接的代碼(如果你把它包含在問題中會很好)并做了一些改動: $nameFile = $_FILES['file']['name'];
$tmpName = $_FILES['file']['tmp_name'];
$download_folder = './files/';
$zip = new ZipArchive();
$fileconpress = $download_f
...
您可能無法將文件移動到/upload/這是服務器文件系統根目錄中的“upload”文件夾,因此move_uploaded_file()報告了FALSE和您的消息。 另外,這個/upload/文件夾可能不存在,也不可寫。 您可能希望將其移至$_SERVER['DOCUMENT_ROOT'].'/upload/' ,它將指向您的虛擬主機根目錄(類似于www或您上傳應用程序文件的任何地方)。 不要忘記創建這個文件夾并相應地更改其權限(CHMOD 777是個好主意)。 You probably can't
...
如果您無法更改服務器環境,請使用其中一種基于Flash的上傳解決方案,如SWFUpload或Uploadify 。 (據我所知,無法使用純Ajax傳輸文件)。 SWFUpload有一個速度插件 ,它可以完成你想要的功能,開箱即用。 If you can't alter your server environment, use one of the Flash based uploading solutions like SWFUpload or Uploadify. (It's not possi
...
這是一個2折的過程,首先是上傳本身然后是服務器上的操作。 第一個驗證是確保文件甚至上傳。 為此你可以在線后使用 $fileName = $_FILES['userfile']['name'];
使用: if(is_uploaded_file($fileName)) {
// Here goes all the file manipulation/storage/etc
} else {
echo 'Error: File could not be uploaded.';
}
嘗試使用它
...
你應該檢查錯誤: 1 = UPLOAD_ERR_INI_SIZE值:1; 上傳的文件超過了php.ini中的upload_max_filesize指令。 rtm: http : //php.net/manual/en/features.file-upload.errors.php 你重啟了Apache嗎? You should be checking for errors: 1 = UPLOAD_ERR_INI_SIZE Value: 1; The uploaded file exceeds th
...
我不確定我是否明白你的意思。 如果您只想在將文件存儲到“upload”目錄時重命名該文件,請在使用move_uploaded_file()時執行此操作: $destination = "upload/" . $new_filename;
if (file_exists($destination)) {
echo 'File ', $destination, ' already exists!';
} else {
move_uploaded_file($temp_filename,
...
由于目標文件存在于文件夾中,如果在數據庫中找不到文件,那么顯然應該檢查insert查詢。 請檢查插入查詢的語法 mysql_query("Insert into files values('$name','$file')");
Since the target files are there in folder, and if you can't find the files on the database then obvious thing should be to check insert
...
如果你想一次上傳一個文件而不是用這個替換html name = "userfile" 刪除multiple 它將開始工作 如果你一次上傳多個文件然后用這個替換html,那么你需要運行循環來存儲文件。 請參閱此 如果您在上傳大文件時遇到麻煩,那么您可以將它放在您的php頁面之上 ini_set('upload_max_filesize', '10M');
ini_set('post_max_size', '10M');
ini_set('max_input_time', 300);
ini_set(
...
總結
以上是生活随笔為你收集整理的php中的file_upload,PHP文件上传(PHP file upload)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 两个苹果手机如何同步数据_同步苹果手机和
- 下一篇: php 如何守护进程_PHP 如何实现守