admin管理员组文章数量:1130349
I'm using this code in my plugin to add an extra field to the user profiles so that the user can upload an image. It uploads the file perfectly but the only problem is the part that has to delete the previous uploaded image doesn't work.
This is my code:
<?php
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) {
?>
<script type="text/javascript">
var form = document.getElementById('your-profile');
form.encoding = "multipart/form-data";
form.setAttribute('enctype', 'multipart/form-data');
</script>
<table class="form-table">
<tr>
<th><label for="profile_photo">Upload your profile photo</label></th>
<td>
<p>
<input type="file" name="profile_photo" id="profile_photo" />
<input type="hidden" name="action" value="save">
<input type="submit" name="submitprofilephoto" id="submitprofilephoto" class="button" value="بارگذاری">
</p>
<span class="description">
<?php
$author_profile_photo = get_author_profile_photo($user->ID);
if(is_array($author_profile_photo)):
?>
<a href="<?php echo $author_profile_photo["url"];?>" target="_blank">
<img src="<?php echo $author_profile_photo["file"];?>" height="100" width="100" />
</a>
<?php
endif;
?>
</span>
</td>
</tr>
</table>
<?php
}
function get_author_profile_photo($user_ID) {
$author_data = get_the_author_meta( 'profile_photo', $user_ID );
$uploads = wp_upload_dir();
$author_data["file"] = $uploads["baseurl"] . $author_data["file"];
return $author_data;
}
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
function my_save_extra_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
$upload=$_FILES['profile_photo'];
$uploads = wp_upload_dir();
if(isset($_POST) && $_POST['submitprofilephoto']!='') {
if ($upload['tmp_name'] && file_is_displayable_image( $upload['tmp_name'] )) {
// handle the uploaded file
$overrides = array('test_form' => false);
$file=wp_handle_upload($upload, $overrides);
$file["file"] = $uploads["subdir"]."/".basename($file["url"]);
// Setup the array of supported file types. In this case, it's just Images.
$supported_types = array( 'image/jpeg', 'image/pjpeg', 'image/png' );
// Get the file type of the upload
$arr_file_type = wp_check_filetype(basename($upload['name']));
$uploaded_type = $arr_file_type['type'];
// Check if the type is supported. If not, throw an error.
if( $file && in_array($uploaded_type, $supported_types) ) {
if($upload['size'] > 204800) {
wp_die('<strong>ERROR</strong>: Maximum size allowed is 200KB.');
}
//remove previous uploaded file
$author_profile_photo = get_author_profile_photo($user_id);
@unlink($author_profile_photo["file"]);
// I even tried unlink without the @ and it still doesn't delete the file!
update_user_meta( $user_id, 'profile_photo', $file );
} else wp_die('<strong>ERROR</strong>: Allowd image formats are JPEG, JPG and PNG.');
} elseif (!file_is_displayable_image( $upload['tmp_name'] )) wp_die('<strong>ERROR</strong>: The file you selected is not an image!');
}
}
?>
Where or what is the problem?
I'm using this code in my plugin to add an extra field to the user profiles so that the user can upload an image. It uploads the file perfectly but the only problem is the part that has to delete the previous uploaded image doesn't work.
This is my code:
<?php
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) {
?>
<script type="text/javascript">
var form = document.getElementById('your-profile');
form.encoding = "multipart/form-data";
form.setAttribute('enctype', 'multipart/form-data');
</script>
<table class="form-table">
<tr>
<th><label for="profile_photo">Upload your profile photo</label></th>
<td>
<p>
<input type="file" name="profile_photo" id="profile_photo" />
<input type="hidden" name="action" value="save">
<input type="submit" name="submitprofilephoto" id="submitprofilephoto" class="button" value="بارگذاری">
</p>
<span class="description">
<?php
$author_profile_photo = get_author_profile_photo($user->ID);
if(is_array($author_profile_photo)):
?>
<a href="<?php echo $author_profile_photo["url"];?>" target="_blank">
<img src="<?php echo $author_profile_photo["file"];?>" height="100" width="100" />
</a>
<?php
endif;
?>
</span>
</td>
</tr>
</table>
<?php
}
function get_author_profile_photo($user_ID) {
$author_data = get_the_author_meta( 'profile_photo', $user_ID );
$uploads = wp_upload_dir();
$author_data["file"] = $uploads["baseurl"] . $author_data["file"];
return $author_data;
}
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
function my_save_extra_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
$upload=$_FILES['profile_photo'];
$uploads = wp_upload_dir();
if(isset($_POST) && $_POST['submitprofilephoto']!='') {
if ($upload['tmp_name'] && file_is_displayable_image( $upload['tmp_name'] )) {
// handle the uploaded file
$overrides = array('test_form' => false);
$file=wp_handle_upload($upload, $overrides);
$file["file"] = $uploads["subdir"]."/".basename($file["url"]);
// Setup the array of supported file types. In this case, it's just Images.
$supported_types = array( 'image/jpeg', 'image/pjpeg', 'image/png' );
// Get the file type of the upload
$arr_file_type = wp_check_filetype(basename($upload['name']));
$uploaded_type = $arr_file_type['type'];
// Check if the type is supported. If not, throw an error.
if( $file && in_array($uploaded_type, $supported_types) ) {
if($upload['size'] > 204800) {
wp_die('<strong>ERROR</strong>: Maximum size allowed is 200KB.');
}
//remove previous uploaded file
$author_profile_photo = get_author_profile_photo($user_id);
@unlink($author_profile_photo["file"]);
// I even tried unlink without the @ and it still doesn't delete the file!
update_user_meta( $user_id, 'profile_photo', $file );
} else wp_die('<strong>ERROR</strong>: Allowd image formats are JPEG, JPG and PNG.');
} elseif (!file_is_displayable_image( $upload['tmp_name'] )) wp_die('<strong>ERROR</strong>: The file you selected is not an image!');
}
}
?>
Where or what is the problem?
Share Improve this question edited Oct 21, 2013 at 7:29 aminb5 asked Oct 20, 2013 at 15:51 aminb5aminb5 11 silver badge3 bronze badges 7 | Show 2 more comments3 Answers
Reset to default 0I figured it out! the problem is the path is not right! Thanks to this post : Alow users to delete uploaded images inside backend?
this function below helped :
function url_to_path_test($url){
$url=str_replace(rtrim(get_site_url(),'/').'/', ABSPATH, $url);
return $url;
}
So in my code if you change the remove previous image like this it will work :
//remove previous uploaded file
$author_profile_photo = get_author_profile_photo($user_id);
$url=str_replace(rtrim(get_site_url(),'/').'/', ABSPATH, $author_profile_photo["file"]);
@unlink($url);
I realized that the code below is a much better solution :
//remove previous uploaded file
$author_data = get_the_author_meta( 'profile_photo', $user_id );
$author_data["file"] = $uploads["path"] . $author_data["file"];
$imagepath = $author_data["file"];
@unlink($imagepath);
Here give full url of your file where it is located.
unlink(public_path('file/to/delete'))
as you are just providing file name in your code
@unlink($author_profile_photo["file"]);
and with if you want to prevent generating an error if the file does not exist.
I'm using this code in my plugin to add an extra field to the user profiles so that the user can upload an image. It uploads the file perfectly but the only problem is the part that has to delete the previous uploaded image doesn't work.
This is my code:
<?php
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) {
?>
<script type="text/javascript">
var form = document.getElementById('your-profile');
form.encoding = "multipart/form-data";
form.setAttribute('enctype', 'multipart/form-data');
</script>
<table class="form-table">
<tr>
<th><label for="profile_photo">Upload your profile photo</label></th>
<td>
<p>
<input type="file" name="profile_photo" id="profile_photo" />
<input type="hidden" name="action" value="save">
<input type="submit" name="submitprofilephoto" id="submitprofilephoto" class="button" value="بارگذاری">
</p>
<span class="description">
<?php
$author_profile_photo = get_author_profile_photo($user->ID);
if(is_array($author_profile_photo)):
?>
<a href="<?php echo $author_profile_photo["url"];?>" target="_blank">
<img src="<?php echo $author_profile_photo["file"];?>" height="100" width="100" />
</a>
<?php
endif;
?>
</span>
</td>
</tr>
</table>
<?php
}
function get_author_profile_photo($user_ID) {
$author_data = get_the_author_meta( 'profile_photo', $user_ID );
$uploads = wp_upload_dir();
$author_data["file"] = $uploads["baseurl"] . $author_data["file"];
return $author_data;
}
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
function my_save_extra_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
$upload=$_FILES['profile_photo'];
$uploads = wp_upload_dir();
if(isset($_POST) && $_POST['submitprofilephoto']!='') {
if ($upload['tmp_name'] && file_is_displayable_image( $upload['tmp_name'] )) {
// handle the uploaded file
$overrides = array('test_form' => false);
$file=wp_handle_upload($upload, $overrides);
$file["file"] = $uploads["subdir"]."/".basename($file["url"]);
// Setup the array of supported file types. In this case, it's just Images.
$supported_types = array( 'image/jpeg', 'image/pjpeg', 'image/png' );
// Get the file type of the upload
$arr_file_type = wp_check_filetype(basename($upload['name']));
$uploaded_type = $arr_file_type['type'];
// Check if the type is supported. If not, throw an error.
if( $file && in_array($uploaded_type, $supported_types) ) {
if($upload['size'] > 204800) {
wp_die('<strong>ERROR</strong>: Maximum size allowed is 200KB.');
}
//remove previous uploaded file
$author_profile_photo = get_author_profile_photo($user_id);
@unlink($author_profile_photo["file"]);
// I even tried unlink without the @ and it still doesn't delete the file!
update_user_meta( $user_id, 'profile_photo', $file );
} else wp_die('<strong>ERROR</strong>: Allowd image formats are JPEG, JPG and PNG.');
} elseif (!file_is_displayable_image( $upload['tmp_name'] )) wp_die('<strong>ERROR</strong>: The file you selected is not an image!');
}
}
?>
Where or what is the problem?
I'm using this code in my plugin to add an extra field to the user profiles so that the user can upload an image. It uploads the file perfectly but the only problem is the part that has to delete the previous uploaded image doesn't work.
This is my code:
<?php
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) {
?>
<script type="text/javascript">
var form = document.getElementById('your-profile');
form.encoding = "multipart/form-data";
form.setAttribute('enctype', 'multipart/form-data');
</script>
<table class="form-table">
<tr>
<th><label for="profile_photo">Upload your profile photo</label></th>
<td>
<p>
<input type="file" name="profile_photo" id="profile_photo" />
<input type="hidden" name="action" value="save">
<input type="submit" name="submitprofilephoto" id="submitprofilephoto" class="button" value="بارگذاری">
</p>
<span class="description">
<?php
$author_profile_photo = get_author_profile_photo($user->ID);
if(is_array($author_profile_photo)):
?>
<a href="<?php echo $author_profile_photo["url"];?>" target="_blank">
<img src="<?php echo $author_profile_photo["file"];?>" height="100" width="100" />
</a>
<?php
endif;
?>
</span>
</td>
</tr>
</table>
<?php
}
function get_author_profile_photo($user_ID) {
$author_data = get_the_author_meta( 'profile_photo', $user_ID );
$uploads = wp_upload_dir();
$author_data["file"] = $uploads["baseurl"] . $author_data["file"];
return $author_data;
}
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
function my_save_extra_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
$upload=$_FILES['profile_photo'];
$uploads = wp_upload_dir();
if(isset($_POST) && $_POST['submitprofilephoto']!='') {
if ($upload['tmp_name'] && file_is_displayable_image( $upload['tmp_name'] )) {
// handle the uploaded file
$overrides = array('test_form' => false);
$file=wp_handle_upload($upload, $overrides);
$file["file"] = $uploads["subdir"]."/".basename($file["url"]);
// Setup the array of supported file types. In this case, it's just Images.
$supported_types = array( 'image/jpeg', 'image/pjpeg', 'image/png' );
// Get the file type of the upload
$arr_file_type = wp_check_filetype(basename($upload['name']));
$uploaded_type = $arr_file_type['type'];
// Check if the type is supported. If not, throw an error.
if( $file && in_array($uploaded_type, $supported_types) ) {
if($upload['size'] > 204800) {
wp_die('<strong>ERROR</strong>: Maximum size allowed is 200KB.');
}
//remove previous uploaded file
$author_profile_photo = get_author_profile_photo($user_id);
@unlink($author_profile_photo["file"]);
// I even tried unlink without the @ and it still doesn't delete the file!
update_user_meta( $user_id, 'profile_photo', $file );
} else wp_die('<strong>ERROR</strong>: Allowd image formats are JPEG, JPG and PNG.');
} elseif (!file_is_displayable_image( $upload['tmp_name'] )) wp_die('<strong>ERROR</strong>: The file you selected is not an image!');
}
}
?>
Where or what is the problem?
Share Improve this question edited Oct 21, 2013 at 7:29 aminb5 asked Oct 20, 2013 at 15:51 aminb5aminb5 11 silver badge3 bronze badges 7-
4
The WordPress way would be
wp_delete_attachment($attachment_id). Have you tried that? – fuxia ♦ Commented Oct 20, 2013 at 15:54 - 1 Hi and welcome! Kudos for showing what you did try. Just a hint for your next Questions: try to isolate the problem before posting the code, see the guide SSCCE. If I were you, I'd follow @toscho's advice, and if you can't make it work, you can update the Question (edit) with the new code. – brasofilo Commented Oct 20, 2013 at 17:15
- how do I get the attachment id?! – aminb5 Commented Oct 20, 2013 at 17:59
- I don't think the code above does give the uploaded file an id?! does it? if it does, then how are we suppose to get the id of it?! – aminb5 Commented Oct 20, 2013 at 18:11
- Please write your solution as an answer and accept this answer. – fuxia ♦ Commented Oct 21, 2013 at 7:07
3 Answers
Reset to default 0I figured it out! the problem is the path is not right! Thanks to this post : Alow users to delete uploaded images inside backend?
this function below helped :
function url_to_path_test($url){
$url=str_replace(rtrim(get_site_url(),'/').'/', ABSPATH, $url);
return $url;
}
So in my code if you change the remove previous image like this it will work :
//remove previous uploaded file
$author_profile_photo = get_author_profile_photo($user_id);
$url=str_replace(rtrim(get_site_url(),'/').'/', ABSPATH, $author_profile_photo["file"]);
@unlink($url);
I realized that the code below is a much better solution :
//remove previous uploaded file
$author_data = get_the_author_meta( 'profile_photo', $user_id );
$author_data["file"] = $uploads["path"] . $author_data["file"];
$imagepath = $author_data["file"];
@unlink($imagepath);
Here give full url of your file where it is located.
unlink(public_path('file/to/delete'))
as you are just providing file name in your code
@unlink($author_profile_photo["file"]);
and with if you want to prevent generating an error if the file does not exist.
本文标签: custom fieldunlink() doesn39t delete the uploaded file
版权声明:本文标题:custom field - unlink() doesn't delete the uploaded file 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749096487a2315335.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


wp_delete_attachment($attachment_id). Have you tried that? – fuxia ♦ Commented Oct 20, 2013 at 15:54