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.
Related
There are some posts here, but they don't work for me. My need is described in the comments. As a poor beginner, I read many possible online manuals, but can't find the solution.
<?php
// BASIC STRUCTURE //
// ./core/
// ./core/index.gen
// ./data/
// ./data/<dir_name>
// ./data/<dir_name>/<file_name>
// ./data/<dir_name>/index.php - for <dir_name> listing
// ./data/index.php - for ./data/ listing
// BASIC OPERATION
// 1. create a subdirectory in ./data/
// 2. copy the ./core/index.gen to new created subdirectory
// 3. create new file in new created directory in ./data/
$base_dir = './data';
$dir_name = '';
$file_name = '';
$data_dir = $_GET['data_dir'];
//Check if the directory with the name already exists
if (!is_dir($base_dir.$data_dir)) {
//Create our directory if it does not exist
mkdir($base_dir.'/'.$data_dir);
$msg = 'SUCCESS';
touch($base_dir.'/'.$data_dir.'/'.$_GET['file_name'].'.html');
// copy index.gen from ./core/ to new created dir
copy($base_dir.'dir_index.php',$base_dir.'/'.$data_dir.'.index.php');
}else{
$msg = 'ERROR';
}
// read directory content
function getFiles(){
$items=array();
if($dir=opendir('.')){
while($item=readdir($dir)){
if($item!='.'
&& $item!='..'
&& $item!=basename(__FILE__)
&& $item != "index.php"
&& $item != "core"
){
$items[]=$item;
}
}
closedir($dir);
}
natsort($items); //sort
return $items;
}
?>
<!-- in HTML bobdy -->
...
<div class="msg"><?php echo $msg;?></div>
<div class="frames">
<div id="dirs" class="frame">
<div class="frame-title">Directories</div>
<!-- generated dirs-list-links targeted to #files -->
<iframe name="dirs" src="<?php echo $base_dir.'/index.php';?>"></iframe>
</div>
<div id="files" class="frame">
<div class="frame-title">Files</div>
<!-- generated files-list-links targeted to #file_preview -->
<iframe name="files" src="<?php echo $data_dir.'/index.php';?>"></iframe>
</div>
<div id="file_preview" class="frame">
<div class="frame-title">Preview</div>
<iframe name="file_preview"><file.html></iframe>
</div>
</div>
The directories and files are created correctly. But the "index" file is not copoied. Please, hint.
I believe you're looking for file_put_contents
read the index file you want to create into a variable
$current = file_get_contents($file);
then write the index file:
$targetIndexFile = '/newDirectory/index.html';
file_put_contents($targetIndexFile,$current;
That should allow you to effectively move a file from one directory to another.
You must add a slash before dir_index.php:
copy($base_dir . '/' . 'dir_index.php',$base_dir.'/'.$data_dir.'.index.php');
My English Languege is not good. sorry.
I Want Get My Variable Folder Name In mydiv Content and Embed to my php Code (in $flds)?
I do not want to use form.
please help me. thanks
<div id="mydiv">Variable Folder Name</div>
<?php
$flds = $mydivContent; //?????
$all_files = glob("uploads/$flds/*.*");
for ($i=0; $i<count($all_files); $i++) {
$image_name = $all_files[$i];
$supported_format = array('gif','jpg','jpeg','png');
$ext = strtolower(pathinfo($image_name, PATHINFO_EXTENSION));
if (in_array($ext, $supported_format)) {
echo '<img src="'.$image_name .'" alt="'.$image_name.'" />';
}
else {
continue;
}
}
?>
PHP is server side programming language, PHP executes first when you send request to server there is no way to call specific html element of same page by PHP.
Either you should already put value to variable $mydivContent in PHP
or you could use html <form>(s) to get response from client
I am having trouble to let users upload files to my php program running on google app engine. I am trying to follow this google manual.
I am working with codeIgniter 3, I set up following controllers:
Projecteditor.php:
public function index( $cluster_id )
{
if( $this->require_min_level( 1 ) )
{
$project_access_lvl = $this->check_access_lvl( $cluster_id );
if( $project_access_lvl >= 0 )
{
// Upload files to Google Cloud Storage
$my_bucket = 'THE_ID.appspot.com';
$options = ['gs_bucket_name' => $my_bucket]; //todo app id
$upload_url = CloudStorageTools::createUploadUrl('/main.php/upload/handler', $options);
$view_data['upload_url'] = $upload_url;
// Display webpage
$cluster = $this->cluster_model->get_cluster_by_id( $cluster_id )[0];
$project = $this->project_model->get_projects( $cluster['projecten_project_id'])[0];
$view_data['fase_details'] = $this->get_fase_details( $cluster['fase'] );
$view_data['project_name'] = $project['naam'];
echo $this->load->view('templates/header', '', TRUE);
echo $this->load->view('projecteditor/index', ( isset( $view_data ) ) ? $view_data : '', TRUE );
echo $this->load->view('templates/footer', '', TRUE);
}
else
{
// TODO syslog
}
}
}
Upload.php:
public function handler()
{
$my_bucket = 'THE_ID.appspot.com';
# [START move_uploaded_file]
$file_name = $_FILES['uploaded_files']['name'];
$temp_name = $_FILES['uploaded_files']['tmp_name'];
move_uploaded_file($temp_name, "gs://${my_bucket}/${file_name}.txt");
# [END move_uploaded_file]
return sprintf('Your file "%s" has been uploaded.', $file_name);
}
Of course I replaced THE_ID with the correct bucket id.
The form inside the view that corresponds with the Projecteditor controller:
<?php
echo form_open($upload_url, array('enctype' => 'multipart/form-data'));
?>
<div class="col-lg-12">
<div id="my-dropzone" class="dropzone" action="#">
<div class="dropzone-previews"></div>
<button type="submit" class="btn btn-success pull-right"><i class="fa fa-upload"></i> <span class="bold">Upload</span></button>
</div>
</div></div>
<?php echo form_close(); ?>
The files are uploaded by using the jQuery dropbox plugin.
I suspect I entered the wrong relative url inside CloudStorageTools::createUploadUrl but I cannot really find out what I am supposed to enter as the relative URL.
the absolute urls for both controllers are:
Projecteditor.php:
https://example.com/projecteditor/index/SOME_VARIABLE
Upload.php:
https://example.com/main.php/upload/handler
When my code executes I get a post to the Projecteditor.php but not to the Upload.php also the datastore bucket stays empty.
I also used this source about the google storage bucket.
Please let me know if I did something wrong or there are alternative ways (easier ways) to store my data.
Thanks in advance
Apparently there was a misconfiguration in the jQuery dropbox plugin that prevented the files from being uploaded, on the PHP side everything worked fine.
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>
My script pretty much works, however when I run a for loop it fails, in the view:
<?php foreach($pics as $index=>$pic){?>
<td class='Pictures'>
<center>
<?php
$piece = explode('/',$pic["ThumbImg"]);
$string = $piece[5];
?>
<img src='<?php echo base_url(); ?>index.php/Controller/getImage/<?php echo $string; ?>/1' width="100px">
</a>
Controller class - Get image function:
//function to protect images from being accessed directly by obfuscating the URL
function getImage($img_id,$type){
if($this->members->logged_in()){
//code to authenticate user goes here then...
//code to decide which type of file it is/thumb/full size image
if($type==1)
{
$url = $this->data['base_url'].'system/application/images/pic/thumbs/';
}else if($type==0){
$url = $this->data['base_url'].'system/application/images/pic/';
}
$filepath = $url.$img_id;
//send image to web browser.
header("Content-type: image/jpeg");
//get path
$img_handle = imagecreatefromjpeg($filepath) or die("");
//create and send
ImageJpeg($img_handle);
}
I suspect that this could be due to the headers being resent, if so how can I resolve this.
Cheers
Fixed the problem - used "Hotlinking"
You will basically only need to set the header information header("Content-type: image/jpeg") if you're loading the image from the database. But since you're loading it from a folder you might want to skip the php file and immediately fetch the image from the folder:
<img src='<?php echo base_url(); ?>img_folder/<?php echo $string; ?>/1'
width="100px">