php中的file_upload,PHP文件上传(PHP file upload)
PHP文件上傳(PHP file upload)
我正在嘗試使用php將文件上傳到我的服務(wù)器,將它們保存到我的mysql數(shù)據(jù)庫(kù)中,但我無(wú)法讓它工作,這是我正在使用的腳本,我相信它與“$ _FILES有關(guān)“因?yàn)楫?dāng)我把它拿出來(lái)時(shí)”&& $ _FILES ['userfile'] ['size']> 0“腳本開(kāi)始運(yùn)行,但是那個(gè)使用”$ _FILES“的變量沒(méi)有定義。
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
更新時(shí)間:2019-12-08 22:24
最滿意答案
這是一個(gè)2折的過(guò)程,首先是上傳本身然后是服務(wù)器上的操作。 第一個(gè)驗(yàn)證是確保文件甚至上傳。 為此你可以在線后使用
$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.';
}
嘗試使用它,如果文件實(shí)際上傳,可能會(huì)重新發(fā)布。 僅僅因?yàn)? _FILES有內(nèi)容,并不一定意味著文件已上傳到服務(wù)器。
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
相關(guān)問(wèn)答
首先,檢查phpinfo()以查看運(yùn)行時(shí)設(shè)置是什么; 如果他們?nèi)匀伙@示2M (默認(rèn)),Apache可能不遵守.htaccess文件 通常我會(huì)告訴您確保在Apache配置的虛擬主機(jī)聲明中有這個(gè): AllowOverride Options FileInfo
但是在共享主機(jī)上并沒(méi)有給你這些權(quán)限(極不可能)。 您應(yīng)該與您的托管服務(wù)提供商聯(lián)系,看看他們是否可以通過(guò)在虛擬主機(jī)中添加php_value設(shè)置來(lái)為您提高這些限制 或者,按照@Satya的建議,通過(guò)分塊上傳文件(例如一次上傳1MB)解決問(wèn)題。 Fi
...
您不能在文件名中使用管道(|)。 這是您遇到錯(cuò)誤的最可能原因。 嘗試將其更改為下劃線或短劃線。 $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擴(kuò)展: http://www.php.net/manual/en/zip.examples.php 我看了你鏈接的代碼(如果你把它包含在問(wèn)題中會(huì)很好)并做了一些改動(dòng): $nameFile = $_FILES['file']['name'];
$tmpName = $_FILES['file']['tmp_name'];
$download_folder = './files/';
$zip = new ZipArchive();
$fileconpress = $download_f
...
您可能無(wú)法將文件移動(dòng)到/upload/這是服務(wù)器文件系統(tǒng)根目錄中的“upload”文件夾,因此move_uploaded_file()報(bào)告了FALSE和您的消息。 另外,這個(gè)/upload/文件夾可能不存在,也不可寫(xiě)。 您可能希望將其移至$_SERVER['DOCUMENT_ROOT'].'/upload/' ,它將指向您的虛擬主機(jī)根目錄(類似于www或您上傳應(yīng)用程序文件的任何地方)。 不要忘記創(chuàng)建這個(gè)文件夾并相應(yīng)地更改其權(quán)限(CHMOD 777是個(gè)好主意)。 You probably can't
...
如果您無(wú)法更改服務(wù)器環(huán)境,請(qǐng)使用其中一種基于Flash的上傳解決方案,如SWFUpload或Uploadify 。 (據(jù)我所知,無(wú)法使用純Ajax傳輸文件)。 SWFUpload有一個(gè)速度插件 ,它可以完成你想要的功能,開(kāi)箱即用。 If you can't alter your server environment, use one of the Flash based uploading solutions like SWFUpload or Uploadify. (It's not possi
...
這是一個(gè)2折的過(guò)程,首先是上傳本身然后是服務(wù)器上的操作。 第一個(gè)驗(yàn)證是確保文件甚至上傳。 為此你可以在線后使用 $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.';
}
嘗試使用它
...
你應(yīng)該檢查錯(cuò)誤: 1 = UPLOAD_ERR_INI_SIZE值:1; 上傳的文件超過(guò)了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
...
我不確定我是否明白你的意思。 如果您只想在將文件存儲(chǔ)到“upload”目錄時(shí)重命名該文件,請(qǐng)?jiān)谑褂胢ove_uploaded_file()時(shí)執(zhí)行此操作: $destination = "upload/" . $new_filename;
if (file_exists($destination)) {
echo 'File ', $destination, ' already exists!';
} else {
move_uploaded_file($temp_filename,
...
由于目標(biāo)文件存在于文件夾中,如果在數(shù)據(jù)庫(kù)中找不到文件,那么顯然應(yīng)該檢查insert查詢。 請(qǐng)檢查插入查詢的語(yǔ)法 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
...
如果你想一次上傳一個(gè)文件而不是用這個(gè)替換html name = "userfile" 刪除multiple 它將開(kāi)始工作 如果你一次上傳多個(gè)文件然后用這個(gè)替換html,那么你需要運(yùn)行循環(huán)來(lái)存儲(chǔ)文件。 請(qǐng)參閱此 如果您在上傳大文件時(shí)遇到麻煩,那么您可以將它放在您的php頁(yè)面之上 ini_set('upload_max_filesize', '10M');
ini_set('post_max_size', '10M');
ini_set('max_input_time', 300);
ini_set(
...
總結(jié)
以上是生活随笔為你收集整理的php中的file_upload,PHP文件上传(PHP file upload)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
 
                            
                        - 上一篇: 两个苹果手机如何同步数据_同步苹果手机和
- 下一篇: dataframe 一列的不同值_pyt
