Not Picking Up Image If False - php

In my codeigniter form, I am trying to set an else if for my data image. Currently it picks up if when image exists. But need it to pick up my no_image.png, if no image exist using the same code below.
<?php
class Users extends MX_Controller {
public function index() {
$this->getForm();
}
protected function getForm($user_id = 0) {
$this->load->model('admin/user/users_model');
$user_info = $this->users_model->getUser($user_id);
if (trim($this->input->post('image'))) {
$data['image'] = $this->input->post('image');
} elseif (!empty($user_info)) {
// This Location Image Works Fine.
$data['image'] = config_item('base_url') . 'image/catalog/' . $user_info['image'];
} else {
// Should Display If No Image Present In Database Does Not Work.
$data['image'] = config_item('base_url') . 'image/'. 'no_image.png';
}
}
$this->load->view('user/users_form', $data);
}
View Page :
<div class="form-group">
<label for="input-image" class="col-lg-2 col-md-12 col-sm-2 col-xs-12"><?php echo $entry_image; ?></label>
<div class="col-lg-10 col-md-10 col-sm-10 col-xs-12">
<a href="" id="thumb-image" data-toggle="image" class="img-thumbnail">
<img src="<?php echo $image; ?>"/>
</a>
</div>
</div>

Make sure that in image folder the no_image.png exists. And it is accessible directly.Like
, you can try directly open the link in your browser as http://www.yoursiteurl/image/no_image.png.
If it opens then there is two possibilities
in your config file there is no end slash for baseurl.
The code never executes the else block
If the dierct link does not open then a htaccess problem may exists.
Hope this will help you.
Or your elseif condition may be like this
elseif(!empty($user_info) && $user_info['image'] && file_exists(config_item('base_url') . 'image/catalog/' . $user_info['image'])){
...
}
// + so your user info is valid but user picture is not available

I had to create a if statement below to make it work for no_image.png now works this way.
if (trim($this->input->post('image'))) {
$data['image'] = $this->input->post('image');
} elseif (!empty($user_info)) {
$data['image'] = config_item('base_url') . 'image/catalog/' . $user_info['image'];
} else {
$data['image'] = '';
}
if ($user_info['image'] == FALSE) {
$data['image'] = config_item('base_url') . 'image/'. 'no_image.png';
}

Related

How correctly refresh included PHP file?

I'm just a persistent beginner and I've met another obstacle in my way, so I hope that you'll help me one more time... :) I've got that HTML:
<div class='hold'><?php include 'image.php'; ?></div>
<div class='refresh'>Get new image</div>
And that PHP code:
<?php
$dir = 'images/';
$img = array();
if(is_dir($dir)) {
if($od = opendir($dir)) {
while(($file = readdir($od)) !== false) {
if(strtolower(strstr($file, '.'))==='.jpg') {
array_push($img, $file);
}
}
closedir($od);
}
}
$id = uniqid();
$smth = array_rand($img, 1);
echo '<img src=' . $dir.$img[$smth] . '?' . $id . ' width="200px" height="50px" />';
echo '<input type="hidden" value=' . $id . ' />';
?>
So now when I'm looking at my page I see in the <div class='hold'></div> my img from the folder images, and it's allright. BUT when I click on the <div class='refresh'></div> I obviously want to get another img from my folder, but I dunno how to accomplish that trick correctly.
I know that first answer will be USE AJAX, and I know that I can do something like
function loadGraphic() {
$('.hold').load('image.php');
};
loadGraphic();
and then $('.refresh').click(loadGraphic); but when I'm trying to do that in response from server I get TWO THINGS: image.php and, of course, something like car.jpg?573c4e010c7f6... But I very-very wanna get just ONE looking like
car.jpg?573c4e010c7f6
or
image.php?573c4e010c7f6 - I don't care...
So... I hope you've got my concept... maybe I'm asking for miracle - I dunno! Any, absolutely any help will be appreciated :)
Try it the following way:
JS function:
function loadGraphic() {
$.get("image.php", function(data) {
$(".hold").html(data);
});
};
Html:
<div class='refresh' onclick='loadGraphic()'>Get new image</div>

Variable for file directory path not being recognized

so far I've successfully moved an uploaded image to its designated directory and stored the file path of the moved image into a database I have.
Problem is, however, is that the img src I have echoed fails to read the variable containing the file path of the image. I've been spending time verifying the validity of my variables, the code syntax in echoing the img src, and the successful execution of the move/storing code, but I still get <img src='' when I refer to the view source of the page that is supposed to display the file path contained in the variable.
I believe the file path is stored within the variable because the variable was able to be recognized by the functions that both moved the image to a directory and the query to database.
My coding and troubleshooting experience is still very adolescent, thus pardon me if the nature of my question is bothersomely trivial.
Before asking this question, I've searched for questions within SOF but none of the answers directly addressed my issue (of the questions I've searched at least).
Main PHP Block
//assigning post values to simple variables
$location = $_POST['avatar'];
.
.
.
//re-new session variables to show most recent entries
$_SESSION["avatar"] = $location;
.
.
.
if (is_uploaded_file($_FILES["avatar"]["tmp_name"])) {
//define variables relevant to image uploading
$type = explode('.', $_FILES["avatar"]["name"]);
$type = $type[count($type)-1];
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$rdn = substr(str_shuffle($chars), 0, 15);
//check image size
if($_FILES["avatar"]["size"] > 6500000) {
echo"Image must be below 6.5 MB.";
unlink($_FILES["avatar"]["tmp_name"]);
exit();
}
//if image passes size check continue
else {
$location = "user_data/user_avatars/$rdn/".uniqid(rand()).'.'.$type;
mkdir("user_data/user_avatars/$rdn/");
move_uploaded_file( $_FILES["avatar"]["tmp_name"], $location);
}
}
else {
$location = "img/default_pic.jpg";
}
HTML Block
<div class="profileImage">
<?php
echo "<img src='".$location."' class='profilePic' id='profilePic'/>";
?><br />
<input type="file" name="avatar" id="avatar" accept=".jpg,.png,.jpeg"/>
.
.
.
View Source
<div class="profileImage">
<img src='' class='profilePic' id='profilePic'/><br />
<input type="file" name="avatar" id="avatar" accept=".jpg,.png,.jpeg"/>
.
.
.
Alright, I've finally found the error and was able to successfully solve it!
Simply declare a avatar session variable to the $location variable after updating the table, update the html insert by replacing all $location variables with $_SESSION["avatar_column"] and you are set!
PHP:
$updateCD = "UPDATE users SET languages=?, interests=?, hobbies=?, bio=?, personal_link=?, country=?, avatar=? WHERE email=?";
$updateST = $con->prepare($updateCD);
$updateST->bind_param('ssssssss', $lg, $it, $hb, $bio, $pl, $ct, $location, $_SESSION["email_login"]);
$updateST->execute();
$_SESSION["avatar"] = $location; //Important!
if ($updateST->errno) {
echo "FAILURE!!! " . $updateST->error;
}
HTML:
<div class="profileImage">
<?php
$_SESSION["avatar"] = (empty($_SESSION["avatar"])) ? "img/default_pic.jpg" : $_SESSION["avatar"] ;
echo "<img src='".$_SESSION["avatar"]."' class= 'profilePic' id='profilePic'> ";
?>
.
.
.
Thank you!
try this code
<?php
error_reporting(E_ALL);
ini_set('display_errors','on');
$location = ""; //path
if($_POST && $_FILES)
{
if(is_uploaded_file())
{
// your code
if(<your condition >)
{
}
else
{
$location = "./user_data/user_avatars/".$rdn."/".uniqid(rand()).'.'.$type;
if(!is_dir("./user_data/user_avatars/".$rdn."/"))
{
mkdir("./user_data/user_avatars/".$rdn."/",0777,true);
}
move_uploaded_file( $_FILES["avatar"]["tmp_name"], $location);
}
}
else
{
$location = "img/default_pic.jpg";
}
}
?>
Html Code :-
<div>
<?php
$location = (empty($location)) ? "img/default_pic.jpg" : $location ;
echo "<img src='".location."' alt='".."'> ";
?>
</div>
If it helpful don't forget to marked as answer so another can get correct answer easily.
Good Luck..

How pass angularjs array value into php function as a variable?

I have the code which return json response but I don't know how pass the that value in php function.
Code:
<div class="container" ng-controller="askbyctrl" >
<div class="row" ng-repeat="q in qa.data" >
<div class="col-md-12 col-xs-12 col.sm.12" >
<div class="artist-data pull-left" style = "background-color:#BEB7B7;">
<div class="artst-pic pull-left">
<?php
function cache_image($id){
//replace with cache directory
$image_path = 'C:\xampp\htdocs\QA_UI\images';
$image_url = 'http://localhost:8000/api/image/' + id;
//get the name of the file
$exploded_image_url = explode("/",$image_url);
$image_filename = 'id.jpg';
$exploded_image_filename = explode(".",$image_filename);
$extension = end($exploded_image_filename);
//make sure its an image
if($extension=="jpg") {
//get the remote image
$image_to_fetch = file_get_contents($image_url);
if($image_to_fetch == '{"error":true,"details":"No image."}') {
echo 'file not found';
} else {
//save it
$local_image_file = fopen($image_path.'/'.$image_filename, 'w+');
chmod($image_path.'/'.$image_filename,0755);
fwrite($local_image_file, $image_to_fetch);
fclose($local_image_file);
echo 'copied';
}
}
//}
?>
<img ng-src="images/{{q.userID}}.jpg" alt="" class="img-responsive" />
</div>
Please see the code how can I pass the {{q.userID}} in php function..
You can't do it that way. PHP code is running on server side and angular-code in client-side, in browser. That is, when server returns compiled html to browser it has already done all php-code in that file.
If you want to check if image exists and then cache it, you can do it from angular for example with $http. Call with $http your backend-script, which does what you want, and then returns if it was success or not.

slug its not working in codeigniter

i want url slug like this
tkd/index.php/article/77/Kejurnas-2013
but when i click button like this
tkd/index.php/article/77/Kejurnas%202013
what wrong with my code. this is my view
<div class="post-buttons-wrap">
<div class="post-buttons">
<a href="<?php echo base_url('index.php/article/' . intval($dt->id) . '/' . e($dt->slug)) ; ?>" class="ui black label">
Read More
</a>
</div><!-- end .post-buttons -->
</div>
and this is my controller
public function index()
{
$this->data['berita'] = $this->mberita->get_berita();
$this->data['halaman'] = $this->mhalaman->get_profil();
dump('Page!');
// Fetch the page template
$this->data['page'] = $this->page_m->get_by(array('slug' => (string) $this->uri->segment(1)), TRUE);
count($this->data['page']) || show_404(current_url());
// Fetch the page data
$method = '_' . $this->data['page']->template;
if (method_exists($this, $method)) {
$this->$method();
}
else {
log_message('error', 'Could not load template ' . $method .' in file ' . __FILE__ . ' at line ' . __LINE__);
show_error('Could not load template ' . $method);
}
$this->data['contents'] = $this->data['page']->template;
$this->load->view('template/wrapper_mahasiswa', $this->data);
}
please help me what to do. thank you.
You have to use the helper "URL" and use the functions "url_title()" and "convert_accented_characters()" if you need to convert some special characters.
http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html for more info
Example:
In your controller code, you will capture your form inputs. then you have to create the slug with a line like this:
$event['slug'] = url_title(convert_accented_characters($event['eventName']),'-',TRUE);
This line will return you a slug separated by "-", setting all the words to lowercase and deleting all special characters too.
try this- use rawurlencode while working with strings in url-
<a href="<?php echo base_url('index.php/article/' . intval($dt->id) . rawurldecode('/') . e($dt->slug)) ; ?>" class="ui black label">
Read More
</a>

Php image show issue

I'm confused! Basically i've a form with 3 field ex: subject, image, message. So,
(1) If i upload image it's should be show image,
(2) if i don't upload image it's should be show just only subject and message no image box and
(3) if i don't upload image and if there are a youtube link in message box then it's should be show the vedio. **
Following is my php code, but i can't solved the number 3 point! **
<?php
if(empty($imgname))
{
echo '';
}
elseif(!empty($imgname))
{
echo "<span class='vedio'>";
echo '<img src="' . $upload_path . '/' . $imgname . '" width="195" height="130"
style=" background-color:f2f4f6;" />';
echo "</span>";
}
else
{
echo "<span class='vedio'>";
require_once("func.php");
if (preg_match('%(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)
([^"&?/ ]{11})%i', $msg, $match))
{
// echo $video_id = $match[1];
}
echo #get_youtube_embed($video_id = $match[1]);
echo "</span>";
}
?>
Any Idea or Solution that would be better for me.
Shibbir.
You'll never get into your else statement because of your preceding if conditions. In ALL cases, $imgname will either be empty, or not empty.
To demonstrate further:
if (empty($imgname)) {
// ... First condition
} else if (!empty($imgname)) {
// ... Last possible condition
} else {
// Will never reach here because $imgname will
// fall into one of the two of the above conditionals.
}
It's difficult to add to your code without full context, but it seems you may want to include the following after the above code (not with):
if(!empty($msg) && preg_match('%(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)
([^"&?/ ]{11})%i', $msg, $match)) {
require_once("func.php");
echo "<span class='vedio'>";
echo #get_youtube_embed($match[1]);
echo "</span>";
}

Categories