搜索

PHP文件上传

一、普通文件上传方式

使用原生态的php上传文件。

(1)前端代码

    

?

(2)php代码(upload_file.php)

    <?php if ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg"))&& ($_FILES["file"]["size"] < 20000)){        if ($_FILES["file"]["error"] > 0) {          echo "Return Code: " . $_FILES["file"]["error"] . "<br>";        }else {            echo "Upload: " . $_FILES["file"]["name"] . "<br>";            echo "Type: " . $_FILES["file"]["type"] . "<br>";            echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br>";            echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";                  if (file_exists("upload/" . $_FILES["file"]["name"])){               echo $_FILES["file"]["name"] . " already exists. ";            }else{               move_uploaded_file($_FILES["file"]["tmp_name"],               "upload/" . $_FILES["file"]["name"]);               echo "Stored in: " . "upload/" . $_FILES["file"]["name"];            }        }            }else {        echo "Invalid file";      }      ?>  
?

?

?

二、异步文件上传方式

目的是尽量少使用插件。于是使用iframe异步上传文件

?

(1)前端html

    
导入文件:

?

    function startUpload() {          var spanObj = document.getElementById("info");          spanObj.innerHTML = " 开始上传";          document.getElementById("upForm").sumbit();      }      //回调      function stopUpload(responseText){          var spanObj = document.getElementById("info");          spanObj.innerHTML = "上传成功";          spanObj.innerHTML = responseText;      }  

?

(2)服务器端代码

    $file = $_FILES['myfile'];      $fileName = uploadFile($file);      //$result = readFromFile("../upload/" . $fileName);      echo "<script type="text/javascript">window.top.window.stopUpload('{$fileName}')</script>";            function uploadFile($file) {          // 上传路径               $destinationPath = "../upload/";          if (!file_exists($destinationPath)){              mkdir($destinationPath , 0777);          }          //重命名          $fileName = date('YmdHis') . '_' . iconv('utf-8' , 'gb2312' , basename($file['name']));          if (move_uploaded_file($file['tmp_name'], $destinationPath . $fileName)) {              return iconv('gb2312' , 'utf-8' , $fileName);          }          return '';      }  

?

    //代码注释      /*     1,关于basename方法     $path = "/testweb/home.php";     //显示带有文件扩展名的文件名     echo basename($path);     //显示不带有文件扩展名的文件名     echo basename($path,".php");          2,关于iconv     iconv('gb2312' , 'utf-8' , $fileName);//将$fileName从gb2312转为utf-8格式。     注:该函数需要开启php.ini里面的php_iconv.dll          3,关于$_FILES['myfile']     $_FILES相当于一个二维数组,而$_FILES['myfile']相当于一个一维数组。所以可以     $f = $_FILES['myfile'];     echo $f['name'];          如果直接访问该$_FILES['myfile'],则会报Undefined index: myfile。此时加上     if(!isset($_FILES['myfile'])){         die('上传文件不存在!');     }     */  
?
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
可以在PHP会话中存储哪些数据?可以在PHP会话中存储哪些数据?May 02, 2025 am 12:17 AM

phpsessionscanStorestrings,数字,数组和原始物。

您如何开始PHP会话?您如何开始PHP会话?May 02, 2025 am 12:16 AM

tostartaphpsession,usesesses_start()attheScript'Sbeginning.1)placeitbeforeanyOutputtosetThesessionCookie.2)useSessionsforuserDatalikeloginstatusorshoppingcarts.3)regenerateSessiveIdStopreventFentfixationAttacks.s.4)考虑使用AttActAcks.s.s.4)

什么是会话再生,如何提高安全性?什么是会话再生,如何提高安全性?May 02, 2025 am 12:15 AM

会话再生是指在用户进行敏感操作时生成新会话ID并使旧ID失效,以防会话固定攻击。实现步骤包括:1.检测敏感操作,2.生成新会话ID,3.销毁旧会话ID,4.更新用户端会话信息。

使用PHP会话时有哪些性能考虑?使用PHP会话时有哪些性能考虑?May 02, 2025 am 12:11 AM

PHP会话对应用性能有显着影响。优化方法包括:1.使用数据库存储会话数据,提升响应速度;2.减少会话数据使用,只存储必要信息;3.采用非阻塞会话处理器,提高并发能力;4.调整会话过期时间,平衡用户体验和服务器负担;5.使用持久会话,减少数据读写次数。

PHP会话与Cookie有何不同?PHP会话与Cookie有何不同?May 02, 2025 am 12:03 AM

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

PHP如何识别用户的会话?PHP如何识别用户的会话?May 01, 2025 am 12:23 AM

phpientifiesauser'ssessionusessessionSessionCookiesAndSessionIds.1)whiwSession_start()被称为,phpgeneratesainiquesesesessionIdStoredInacookInAcookInamedInAcienamedphpsessidontheuser'sbrowser'sbrowser.2)thisIdAllowSphptptpptpptpptpptortoreTessessionDataAfromtheserverMtheserver。

确保PHP会议的一些最佳实践是什么?确保PHP会议的一些最佳实践是什么?May 01, 2025 am 12:22 AM

PHP会话的安全可以通过以下措施实现:1.使用session_regenerate_id()在用户登录或重要操作时重新生成会话ID。2.通过HTTPS协议加密传输会话ID。3.使用session_save_path()指定安全目录存储会话数据,并正确设置权限。

PHP会话文件默认存储在哪里?PHP会话文件默认存储在哪里?May 01, 2025 am 12:15 AM

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版