Reputation:
I'm trying to upload, rename a file, then send to dB, so I can display the cropped image. In order for the cropped image to correspond to the user who's logged in, I need to include user specific data in hidden input fields.
Here is my html:
<div class="modal-body">
<form id="cropimage" method="post" enctype="multipart/form-data" action="change_pic.php">
<strong>Upload Image:</strong> <br><br>
<input type="file" name="profile-pic" id="profile-pic" />
<input type="hidden" name="hdn-profile-id" id="hdn-profile-id" value="<?php echo $user['id']; ?>" />
<input type="hidden" name="username-id" id="username-id" value="<?php echo $user['username']; ?>" />
<input type="hidden" name="hdn-x1-axis" id="hdn-x1-axis" value="" />
<input type="hidden" name="hdn-y1-axis" id="hdn-y1-axis" value="" />
<input type="hidden" name="hdn-x2-axis" value="" id="hdn-x2-axis" />
<input type="hidden" name="hdn-y2-axis" value="" id="hdn-y2-axis" />
<input type="hidden" name="hdn-thumb-width" id="hdn-thumb-width" value="" />
<input type="hidden" name="hdn-thumb-height" id="hdn-thumb-height" value="" />
<input type="hidden" name="action" value="" id="action" />
<input type="hidden" name="image_name" value="" id="image_name" />
<div id='preview-profile-pic'></div>
<div id="thumbs" style="padding:5px; width:600p"></div>
</form>
</div>
The form is being captured here:
function changeProfilePic() {
$post = isset($_POST) ? $_POST: array();
$max_width = "500";
$userId = isset($post['hdn-profile-id']) ? intval($post['hdn-profile-id']) : 0;
$username = isset($post['username-id']);
$path = 'assets/images/tmp';
$valid_formats = array("jpg", "png", "gif", "jpeg");
$name = $_FILES['profile-pic']['name'];
$size = $_FILES['profile-pic']['size'];
if(strlen($name)) {
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats)) {
if($size<(1024*1024)) {
$actual_image_name = 'avatar' . '_' . $username.'_'.$userId .'.'.$ext;
///etc,etc...
What I'm trying to do is get to the build $actual_image_name = 'avatar' . '_' . $username.'_'.$userId .'.'.$ext;
The problem is, $username
is always outputting as 1
vs the persons username. So file names that I'm getting are avatar_1_11.jpg
when they should be avatar_john_smith_11.jpg
I don't understand. If the value of the hidden input is <?php echo $user['username']; ?>
and I'm retrieving that based on id
in function changeProfilePic()
, why would it be evaluating to 1? Am I retrieving this incorrectly?
Upvotes: 1
Views: 41
Reputation: 79014
isset()
is returning true
:
$username = isset($post['username-id']);
Which converts to string 1
when you concatenate it into $actual_image_name
:
$actual_image_name = 'avatar' . '_' . $username.'_'.$userId .'.'.$ext;
You can do it as you did for the other variable:
$username = isset($post['username-id']) ? $post['username-id'] : '';
However, why would it be the case that it is not set and how should you handle that?
Upvotes: 1