php实现文件上传的例子(附详细源码)
今天用PHP做了个文件上传工具,功能还是很完善滴,如下:
每个图片上传之后,都有自己的地址,改变原图位置或重命名时,将不会重复上传。
一、功能:
A:文件分类上传
B:生成相应的文件夹,如本例,选择团,生成文件夹tuan
C:点击图片,查看详细
二、HTML代码:
<div id="container"> <fieldset> <legend class="img"><img src="/static/imghwm/default1.png" data-src="css/logo.png" class="lazy" style="max-width:90%" alt=" php实现文件下传的例子(附详细源码) " ></legend> <form action="" method="post" name="myform" id="myform" onsubmit="return false" enctype="multipart/form-data"> <strong>亲,请选择你要上传的文件</strong> <div class="file"><input type="file" id="fileToUpload" name="fileToUpload"></div> <select id="product"> <option value="" if echo> >请选择产品</option> <option value="tuan" if echo> >团</option> </select> <button id="postBtn">Submit</button> </form> <div id="info"></div> <div style="position:relative; top:40px; left:20px;"> <a id="loading" style="display:none"><img src="/static/imghwm/default1.png" data-src="css/loading.gif" class="lazy" style="max-width:90%" alt=" php实现文件下传的例子(附详细源码) " ></a> </div> <a href="#" id="img_link" target="_blank"> <div id="img_url">buding</div> <br> <img class="preview" id="preview" src="" style="max-width:90%" alt=" php实现文件下传的例子(附详细源码) " > </a> </fieldset> </div>
三、Javascript代码:
<script type="text/javascript"> $('#postBtn').click(function(){ $('#preview').hide(); $('#img_url').hide(); $('#loading') .ajaxStart(function(){ $(this).show(); }) .ajaxComplete(function(){ $(this).hide(); }); if($('#fileToUpload').val() == ""){ $('#info').html("亲,还没选择文件呢"); var jObject={"Url":"","Height":413}; var jString=JSON.stringify(jObject); window.parent.postMessage(jString,'*'); return false; } if($('#product').val() == ""){ $('#info').html("亲,还没选择产品呢"); $('#info').css("color","#e9af32"); var jObject={"Url":"","Height":413}; var jString=JSON.stringify(jObject); window.parent.postMessage(jString,'*'); return false; } var val = $('#product').val(); $.ajaxFileUpload({ url:'ajaxupload.php', secureuri:false, fileElementId:'fileToUpload', dataType: 'text', data:{product:val}, success: function (data, status) { if(data.search(/http:\/\//i) < 0 ){ $('#info').html(data); var jObject={"Url":"","Height":413}; var jString=JSON.stringify(jObject); window.parent.postMessage(jString,'*'); }else{ $('#info').html("您上传的文件为:<br/>"); $('#preview').attr("src",data); $('#img_link').attr("href",data); $('#img_url').html(data); $('#preview').show(); $('#img_url').show(); $('#preview').load(function(){ var imgH=$('#preview').height(); var jObject={"Url":data,"Height":imgH+228,"oid":"<?php echo @$_REQUEST['oid']; ?>"}; var jString=JSON.stringify(jObject); window.parent.postMessage(jString,'*'); }); } }, error: function (data, status, e){ $('#info').html(data+e); } }); }); </script>
四、PHP代码
<?php require_once('config.php'); if(empty($_FILES) || empty($_REQUEST)){ header('location:imgupload.php'); exit; } array_push($_FILES, $_REQUEST); $filename = 'fileToUpload'; $product = @$_FILES[0]['product']; $today = date("Y-m-d"); $time = date("YmdHis"); $year = date("Y"); $month = date("m"); $day = date("d"); $img_path = $product.'/'.$year.'/'.$month.'/'.$day.'/'; $destination_dir = ROOT_PATH.'/pic/'.$img_path.'/'; if(!is_uploaded_file($_FILES[$filename]['tmp_name'])){//验证上传文件是否存在 echo "请选择你想要上传的图片"; exit; } if($product == "") {//选择产品 echo "请选择产品"; exit; } $files = $_FILES[$filename]; if($max_file_size < $files['size']){//判断文件是否超过限制大小 echo "图片太大了,传个小点的吧(<=2MB)"; exit; } if(!file_exists($destination_dir)) {//判断上传目录是否存在,不存在则创建一个. if(!mkdir($destination_dir,0777,true)) { echo "创建目录 {".$destination_dir."} 失败<可能是权限问题>"; exit; } } $type = pathinfo($files['name']); $type = strtolower($type["extension"]); $type =".".$type; $tmp_name = $files['tmp_name']; $md5file = md5_file($tmp_name);//生成md5文件 $new_name =$md5file.$type; $img_relat_path = $img_path.$new_name; $img_abs_path = $destination_dir.$new_name; $url = IMG_URL.$img_relat_path; //判断数据库中图片是否存在 $sql="select url from file_url where md5 = '".$md5file."'"; $res=$db->getOne($sql); if($res) { echo $res['url']; exit; } if(!move_uploaded_file ($files['tmp_name'], $img_abs_path)) {//上传文件 echo "上传文件失败"; exit; } //将图片存入数据库 $sql="insert into file_url(url,product,md5,create_time) values('".$url."','".$product."','".$md5file."','".$today."')"; $db->Execute($sql); $db->CloseDB(); echo $url; ?>

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment