wordpress Wordpress自定义文件上传页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15423879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wordpress custom file upload in page
提问by objectiveccoder001
I'm currently using this code for PHP file upload (found directly on the wordpress page):
我目前正在使用此代码上传 PHP 文件(直接在 wordpress 页面上找到):
<form enctype="multipart/form-data" action="upload.php" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form>
upload.php
上传.php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
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";
}
It always returns "invalid file" even when I know 100% it should work.
即使我知道 100% 它应该工作,它也总是返回“无效文件”。
I've been looking around online and I think that WP cannot do enctype="multipart/form-data" so that's why it doesn't work.
我一直在网上环顾四周,我认为 WP 不能做 enctype="multipart/form-data" 所以这就是它不起作用的原因。
Does anyone have a work around or any idea why this won't work?
有没有人有解决方法或任何想法为什么这行不通?
采纳答案by jogesh_pi
why not use WordPress built-in uploader to upload your file?
Here is a quick tutorial about how to implement WordPress Uploader
为什么不使用 WordPress 内置上传器来上传您的文件?
这是一个关于如何实现WordPress Uploader的快速教程
回答by stinkysGTI
I know this post is kinda old, but hopefully will help others.
我知道这篇文章有点旧,但希望能帮助其他人。
I was doing something very similar (custom uploads to display on a custom page) and also using practically the exact same code as objectiveccoder001
. I kept getting "Invalid file." and write permission errors. I ended up going with this:
我正在做一些非常相似的事情(自定义上传显示在自定义页面上),并且还使用了与objectiveccoder001
. 我一直收到“无效文件”。和写权限错误。我最终选择了这个:
if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
$uploadedfile = $_FILES['file'];
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile ) {
echo "File is valid, and was successfully uploaded.\n";
//var_dump( $movefile);
} else {
echo "Possible file upload attack!\n";
}
Works great if you're not looking to use Wordpress's media uploader and just need a simple file upload. It still uploads it using a dated file structure like the built in uploader. Then you can just use $movefile
array to get the file's data.
如果您不打算使用 Wordpress 的媒体上传器,而只需要简单的文件上传,则效果很好。它仍然使用像内置上传器这样的过时文件结构上传它。然后你可以使用$movefile
数组来获取文件的数据。
Reference: http://codex.wordpress.org/Function_Reference/wp_handle_upload
参考:http: //codex.wordpress.org/Function_Reference/wp_handle_upload
回答by Rutunj sheladiya
YOu can use wordpress default media file uploader by using this code and simply retrieve link of image in jquery
您可以使用此代码使用 wordpress 默认媒体文件上传器,只需在 jquery 中检索图像链接
<label for="upload_image">
<input id="upload_image" type="text" size="36" name="ad_image" value="http://" />
<input id="upload_image_button" class="button" type="button" value="Upload Image" />
<br />Enter a URL or upload an image
</label>
<?php
add_action('admin_enqueue_scripts', 'my_admin_scripts');
function my_admin_scripts() {
if (isset($_GET['page']) && $_GET['page'] == 'my_plugin_page') {
wp_enqueue_media();
wp_register_script('my-admin-js', WP_PLUGIN_URL.'/my-plugin/my-admin.js', array('jquery'));
wp_enqueue_script('my-admin-js');
}
}
?>
<script>
jQuery(document).ready(function($){
var custom_uploader;
$('#upload_image_button').click(function(e) {
e.preventDefault();
//If the uploader object has already been created, reopen the dialog
if (custom_uploader) {
custom_uploader.open();
return;
}
//Extend the wp.media object
custom_uploader = wp.media.frames.file_frame = wp.media({
title: 'Choose Image',
button: {
text: 'Choose Image'
},
multiple: true
});
//When a file is selected, grab the URL and set it as the text field's value
custom_uploader.on('select', function() {
console.log(custom_uploader.state().get('selection').toJSON());
attachment = custom_uploader.state().get('selection').first().toJSON();
$('#upload_image').val(attachment.url);
});
//Open the uploader dialog
custom_uploader.open();
});
});
</script>
回答by MD. Shafayatul Haque
Upload file is very easy in new version of WordPress. Just one line of code:
在新版本的 WordPress 中上传文件非常容易。只需一行代码:
$uploaded_file = wp_upload_bits( $_FILES['file']['name'], null, @file_get_contents( $_FILES['file']['tmp_name'] ) );
$uploaded_fileis an array which will return all the information including url, file type etc
$uploaded_file是一个数组,它将返回所有信息,包括 url、文件类型等