欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

php文件上传入门代码

程序员文章站 2022-04-08 09:33:36
...
php文件上传的入门代码。
<?php
/**
* php 文件上传
* by www.jbxue.com
*/
if(!empty($_FILES["uploadImage"])) {
// get file name
$filename = basename($_FILES['uploadImage']['name']);

// get extension
$ext = substr($filename, strrpos($filename, '.') + 1);

// check for jpg only
if ($ext == "jpg") {
// generate unique file name
$newName = 'images/'.time().'.'.$ext;

// upload files
if ((move_uploaded_file($_FILES['uploadImage']['tmp_name'], $newName))) {

// get height and width for image uploaded
list($width, $height) = getimagesize($newName);

// return json data
echo '{"image" : "'.$newName.'", "height" : "'.$height.'", "width" : "'.$width.'" }';
}
else {
echo '{"error" : "An error occurred while moving the files"}';
}
}
else {
echo '{"error" : "Invalid image format"}';
}
}
?>
相关标签: 文件上传