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(

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值