PHP文件上传不上传 [英] php file uploader not uploading

查看:136
本文介绍了PHP文件上传不上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为上传视频的网站制作上传器。截至目前,它不检查它们是否是视频,只是上传它们。我通过一个简单的表单来选择一个文件并将其提交给 upload.php 。这里是我做这个的HTML:

 < form action =upload.phpmethod =postenctype =multipart / form-datatarget =upload_targetonsubmit =startUpload();> 
视频名称:< input type =textname =nameclass =maininputstyle =width:300px; MAXLENGTH = 80 ><峰; br>
档案:< input name =myfiletype =fileclass =mainbutton/ style =clear:both;>< br>
说明:< br>< / textarea>< br>< textarea cols =43rows =10>
< input type =submitname =submitBtnvalue =Uploadclass =mainbutton/>
< / form>< br>< br>< br>
< p id =f1_upload_process>正在载入...< br />< img src =/ images / loader.gifwidth =20height =20/> < / p为H.
< p id =result>< / p>

< iframe id =upload_targetname =upload_targetsrc =#style =width:0; height:0; border:0px solid #fff;>
< script language =javascripttype =text / javascript>
window.top.window.stopUpload(<?php echo $ result;?>);
< / script> < / iframe中>

以下是我的javascript函数:

  function startUpload(){
document.getElementById('f1_upload_process')。style.visibility ='visible';
返回true;
}
函数stopUpload(成功){
var result ='';
if(success == 1){
document.getElementById('result')。innerHTML =
'< span class =msg>文件上传成功! \ /跨度><峰; br /><峰; br />';

else {
document.getElementById('result')。innerHTML =
'< span class =emsg>文件上传出错! ; \ /跨度><峰; br /><峰; br />';
}
document.getElementById('f1_upload_process')。style.visibility ='hidden';
返回true;



$ b $ p
$ b

最后,这里是upload.php的内容,我用它实际上传文件:

 <?php 
$ result = 0;

$ target_path =videos /;
$ target_path = $ target_path。 basename($ _FILES ['myfile'] ['name']);
$ b $ if(move_uploaded_file($ _ FILES ['myfile'] ['tmp_name'],$ target_path)){
$ result = 1;
}

sleep(1);
?>

我相信问题是用upload.php。问题不在于客户端,事实上在客户端,它上传文件,但我无法找到文件在视频文件夹,或在服务器目录中的任何文件夹。



任何帮助,非常感谢。感谢!

解决方案

这是我使用的,您可以自定义它以适应您的脚本:



只需更改*路径和*变量即可。

 <$ 
$ allowed_filetypes = array('。mov','。mp3','。mp4','。flv'); //这些将是通过验证的文件的类型。
$ max_filesize = 524288; // BYTES中的最大文件大小(目前为0.5MB)。
$ upload_path ='./files/'; //文件将被上传到的位置(当前是文件目录)。

$ filename = $ _FILES ['userfile'] ['name']; //获取文件的名称(包括文件扩展名)。
$ ext = substr($ filename,strpos($ filename,'。'),strlen($ filename)-1); //从文件名获得扩展名

//检查文件类型是否被允许,如果不是DIE并通知用户。
if(!in_array($ ext,$ allowed_filetypes))
die('您试图上传的文件是不允许的。

//现在检查文件大小,如果它太大,那么DIE并通知用户。
if(filesize($ _ FILES ['userfile'] ['tmp_name'])> $ max_filesize)
die('您尝试上传的文件太大。

//检查是否可以上传到指定的路径,如果不是DIE并通知用户。
if(!is_writable($ upload_path))
die('您不能上传到指定的目录,请将其改为777.');

//将文件上传到指定的路径。
if(move_uploaded_file($ _ FILES ['userfile'] ['tmp_name'],$ upload_path。$ filename))
echo'您的文件上传成功,查看文件< a href =' 。$ upload_path。$ filename。'title =Your File> here< / a>'; // 有效。
else
echo'文件上传时发生错误。请再试一次。'; //失败:(。

?>


I'm making an uploader for a website which is designed to upload videos. As of now, it doesn't check if they're videos, it's simply uploads them. I do this through a simple form that selects a file and submits it to upload.php. Here is the HTML which I do this with:

<form action="upload.php" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="startUpload();">
Video Name: <input type="text" name="name" class="maininput" style="width:300px;" maxlength="80"><br>
File: <input name="myfile" type="file" class="mainbutton"/ style="clear:both;"><br>
Description: <br><textarea cols="43" rows="10"></textarea><br>
      <input type="submit" name="submitBtn" value="Upload" class="mainbutton"/>
</form><br><br><br>
<p id="f1_upload_process">Loading...<br/><img src="/images/loader.gif" width="20" height="20" /></p>
<p id="result"></p>

<iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;">
<script language="javascript" type="text/javascript">
window.top.window.stopUpload(<?php echo $result; ?>);
</script> </iframe>

Here are my javascript functions which accompany this:

function startUpload(){
    document.getElementById('f1_upload_process').style.visibility = 'visible';
    return true;
}
function stopUpload(success){
      var result = '';
      if (success == 1){
         document.getElementById('result').innerHTML =
       '<span class="msg">The file was uploaded successfully!<\/span><br/><br/>';
      }
      else {
         document.getElementById('result').innerHTML = 
           '<span class="emsg">There was an error during file upload!<\/span><br/><br/>';
      }
      document.getElementById('f1_upload_process').style.visibility = 'hidden';
      return true;   
}

And finally, here is the contents of upload.php, which I use for actually uploading the file:

<?php
   $result = 0;

   $target_path = "videos/";
   $target_path = $target_path . basename( $_FILES['myfile']['name']); 

    if(move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
      $result = 1;
    }

   sleep(1);
?>

I believe that the issue is with the upload.php. The problem is not with anything client side, it's the fact that in the client, it uploads the file, but I can't find the file in the videos folder, or any folder in the server directory.

Any help is greatly appreciated. Thanks!

解决方案

This is what I use, you can customize it to suit your script:

Simply change the *path and *variables.

<?php
// Configuration - Your Options
$allowed_filetypes = array('.mov','.mp3','.mp4','.flv'); // These will be the types of file that will pass the validation.
$max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB).
$upload_path = './files/'; // The place the files will be uploaded to (currently a 'files' directory).

$filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.

// Check if the filetype is allowed, if not DIE and inform the user.
if(!in_array($ext,$allowed_filetypes))
die('The file you attempted to upload is not allowed.');

// Now check the filesize, if it is too large then DIE and inform the user.
if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
die('The file you attempted to upload is too large.');

// Check if we can upload to the specified path, if not DIE and inform the user.
if(!is_writable($upload_path))
die('You cannot upload to the specified directory, please CHMOD it to 777.');

// Upload the file to your specified path.
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked.
else
echo 'There was an error during the file upload. Please try again.'; // It failed :(.

?>

这篇关于PHP文件上传不上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆