怎么把html文件上传到网络,直接从系统上传文件到网络服务器,而不使用HTML格式在php...

我已经解决了我的问题,下面是我的wordpress插件的最终代码。 1-输入Excel进行wordpress的交(包含文章标题和内容描述图像名) 2-图像文件夹

HTML表单后数据导入的wordpress

Upload the xlsx file to Import data.

Please choose a file:

PHP代码文件:

if(isset($_FILES['xl_file']))

{

//upload file to read data from it

$attach_src=$_FILES['xl_file']['tmp_name'];

$file_name=time(). $_FILES["xl_file"]["name"];

$file_name=str_replace(" ","",$file_name);

$upload = wp_upload_bits($file_name, null, file_get_contents($_FILES["xl_file"]["tmp_name"]));

include_once(plugin_dir_path(__FILE__).'PHPExcel/IOFactory.php');

$inputFileName =$upload['file'];

try {

$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);

} catch(Exception $e) {

echo ('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());

}

//array of post data

$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);

//divide the post array into chunks (for larger arrays)

$listing=array_chunk($sheetData, 50);

$list=array();

$images_list=array();

//Reading images

$dir = plugin_dir_path(__FILE__)."Images/";

//get all the images list

$images_list = glob($dir."*.*",GLOB_NOSORT);

apply_filters('admin_memory_limit', WP_MAX_MEMORY_LIMIT);

foreach($sheetData as $list_data){

$list=array();

// get post data into $list array

foreach($list_data as $list_rec){

$list[]=$list_rec;

}

//$list = single post array

$listing_information = array(

'post_title' => addslashes(wp_strip_all_tags($list[0])),

'post_content' => addslashes($list[1]),

'post_type' => 'post',

'post_status' => 'publish'

);

$listingid =wp_insert_post($listing_information);

//listingid= post ID

//image name that we will found/search in image folder

$listing_image_name=trim($list[2]);

//listing_image_name

$upload_image=array();

//loop through images array to find the required image

foreach($images_list as $img){

//$img = /home/public_html/my_site/wp-content/uploads/2014/06/image_name.jpg

// want to extract only image name from $img

$image_name = explode('/', $img);

$image_name = $image_name[count($image_name)-1];

$needle = '.';

$listing_image_name_dir = substr($image_name, 0, strpos($image_name, $needle));

//match the image name if yes then insert

if($listing_image_name == $listing_image_name_dir){

//uploading the image to wordpress

$upload_image = wp_upload_bits($images_list[0], null, file_get_contents($images_list[0]));

// Check the type of tile. We'll use this as the 'post_mime_type'.

$filetype = wp_check_filetype(basename($upload_image['file']), null);

$attachment = array(

'guid' => $upload_image['file'],

'post_mime_type' => $filetype['type'],

'post_title' => $listing_image_name,

'post_content' => '',

'post_status' => 'inherit'

);

//insert wordpress attachment of uploaded image to get attachmen ID

$attach_id = wp_insert_attachment($attachment, $upload_image['file']);

//generate attachment thumbnail

wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $upload_image['file']));

//set this attachment as post featured image

//listingid= post ID

set_post_thumbnail($listingid, $attach_id);

break;

}//end of if

}//end of foreach

unset($list);

unset($listing_image);

unset($upload_image);

flush();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个完整的 PHP 代码示例,该示例演示了如何使用 Dropzone.js 将文件上传并保存到服务器上。 HTML 文件(index.html): ```html <!DOCTYPE html> <html> <head> <title>Dropzone.js 文件上传示例</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.7.0/min/dropzone.min.css"> </head> <body> <h1>Dropzone.js 文件上传示例</h1> <form action="upload.php" class="dropzone"></form> <script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.7.0/min/dropzone.min.js"></script> </body> </html> ``` PHP 文件(upload.php): ```php <?php // 设置允许上传的文件类型和大小限制 $allowedFileTypes = ['jpg', 'jpeg', 'png', 'gif']; $maxFileSize = 5 * 1024 * 1024; // 5MB // 检查上传的文件是否符合要求 if (!empty($_FILES['file'])) { $file = $_FILES['file']; if ($file['error'] === UPLOAD_ERR_OK && in_array(pathinfo($file['name'], PATHINFO_EXTENSION), $allowedFileTypes) && $file['size'] <= $maxFileSize) { // 生成新的文件名 $newFileName = uniqid() . '.' . pathinfo($file['name'], PATHINFO_EXTENSION); // 保存文件到服务器上 move_uploaded_file($file['tmp_name'], 'uploads/' . $newFileName); // 返回上传成功的信息 echo json_encode([ 'status' => 'success', 'message' => '文件上传成功', 'url' => 'uploads/' . $newFileName ]); } else { // 返回上传失败的信息 echo json_encode([ 'status' => 'error', 'message' => '文件上传失败' ]); } } else { // 返回上传失败的信息 echo json_encode([ 'status' => 'error', 'message' => '文件上传失败' ]); } ``` 上述代码中,首先设置了允许上传的文件类型和大小限制。然后,当用户上传文件时,PHP 脚本将检查上传的文件是否符合要求。如果符合要求,则生成新的文件名并将文件保存到服务器上,最后返回上传成功的信息和文件的 URL;否则,返回上传失败的信息。 请注意,上述代码中使用了 `move_uploaded_file` 函数将上传的文件保存到服务器上。在实际场景中,建议使用更安全的方式来保存文件,例如将文件保存到数据库或使用第三方存储服务(如 Amazon S3)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值