how to upload images without reloading page in modx revolution - image

How to upload images in web context (frontend) without reloading the page in modx revolution? I am trying to use fileupload extra for uploading images but it is reloading the page. Can anyone help me, please?

finally i got the answer. we can do it without using FILEUPLOAD addon by using the simple php code and a small javascript..
php code:-
<?php
$path = "uploads/";
$valid_formats = array("jpg", "png", "gif", "bmp","jpeg");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(1024*1024)) // Image size max 1 MB
{
$actual_image_name = time().".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
echo "image uploaded";
}
else
echo "failed";
}
else
echo "Image file size max 1 MB";
}
else
echo "Invalid file format..";
}
else
echo "Please select image..!";
exit;
}
for other javascript and all u can refer the below link:-
http://www.9lessons.info/2011/08/ajax-image-upload-without-refreshing.html

Related

Put JavaScript variable into PHP with AJAX?

Here's a some of my JavaScript
function myFunction() {
var finalMove = "a1";
$.post("index.php", {postFinalMove:finalMove});
];
Here is my PHP code. The commented lines are just two of the things I've tried.
<?php
session_start();
$db = mysqli_connect("localhost","myUsername","myPassword","abraDB");
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
//if (isset($_POST['postFinalMove']))
//{
// $turn = ($db, $_POST['postFinalMove']);
// echo $turn;
//}
//if ($_POST['postFinalMove'])
//{
// $turn = ($db, $_POST['postFinalMove']);
// echo $turn;
//}
if ($_POST['logout'])
{
session_start();
$_SESSION = array();
session_destroy();
}
mysqli_close($db);
?>
As soon as I uncomment some of the code I've tried, my entire webpage is blank with no errors and no source code, which makes this hard for me to debug. Javascript function fires just fine, its my index.php page that crashes. PHP code was working great until this. Thanks in advance
You should be able to do the following in your PHP script:
if(isset($_POST['postFinalMove'])){
echo $_POST['postFinalMove'];
}
This line is probably causing the error:
// $turn = ($db, $_POST['postFinalMove']);
That statement does not make any sense. What are you trying to store in $turn? If you want to store the $_POST['postFinalMove'] you would have:
$turn = $_POST['postFinalMove'];
Take a look at the GIF image and you will see for yourself..!
Replace your code with this line as and you are good to go then :
$turn = $_POST['postFinalMove'];
echo $turn;
It's just a simple Syntax Issue nothing more than that Dear..! :D

image src is not displaying the page in php

**I'm trying to upload the img path in database and show them in the slider, but the problem is the img src is not showing the page assigned. Can any body tell me why the image src is not loading the page.
my code is below slider.php slider has this image code :<img src='<?php echo "imagetest.php?id=$id" ?> width="200"> the image path couldn't display the page imagetest.php has following code: this page is not showing up in img src of the slider if (empty($_GET['id']) || !is_numeric($_GET['id'])) {
echo 'A valid image file id is required to display the image file.';
exit;
}
$imageId = $_GET['id'];
//connect to mysql database
if ($conn = mysqli_connect('localhost', 'root', 'root', 'test')) {
$content = mysqli_real_escape_string($conn, $content);
$sql = "SELECT type, content FROM images where id = {$imageId}";
if ($rs = mysqli_query($conn, $sql)) {
$imageData = mysqli_fetch_array($rs, MYSQLI_ASSOC);
mysqli_free_result($rs);
} else {
echo "Error: Could not get data from mysql database. Please try again.";
}
//close mysqli connection
mysqli_close($conn);
} else {
echo "Error: Could not connect to mysql database. Please try again.";
}
if (!empty($imageData)) {
// show the image.
header("Content-type: {$imageData['type']}");
echo $imageData['content'];
}
?>
Use file_get_contents
<img src="<?php echo file_get_contents('imagetest.php?id='. $id);?>" width="200"/>
and remove header(), u don't need it.
In php file
function track(){
header('Content-Type: image/gif');
$image = 'image.gif';
echo file_get_contents($image);
}
In view file
<img src="track.php" width="200"/>
Hope it's help ^^

PHP Sessions: Generate a variable and save it for session

I am totally new to php sessions. I am not able to get a simple task done.
This is what I am trying to do:
A visitor of my website gets shown a random image
well this part works so far. But everytime the user goes on to another page (I have this random image shown on all pages) the script generates a new random image to be shown.
What I want to do now is:
Save the random image variable to a session so he will see the same image on each page he visits while he has the session saved.
Here is my working code to get a random image without saving it into sessions. If anybody could help me with how the code should look like so it works with sessions would be awesome. Remember: I am a total newby when it comes to sessions.
As you can see I need the variable $img to be stored into a session after it got generated. And the script only to strt again on a new site visit if a user hasnt stored the $img variable in his session.
<?php
function getImagesFromDir($path) {
$images = array();
if ( $img_dir = #opendir($path) ) {
while ( false !== ($img_file = readdir($img_dir)) ) {
// checks for gif, jpg, png
if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
$images[] = $img_file;
}
}
closedir($img_dir);
}
return $images;
}
function getRandomFromArray($ar) {
mt_srand( (double)microtime() * 1000000 ); // php 4.2+ not needed
$num = array_rand($ar);
return $ar[$num];
}
$root = '';
// use if specifying path from root
//$root = $_SERVER['DOCUMENT_ROOT'];
$path = 'images/';
// Obtain list of images from directory
$imgList = getImagesFromDir($root . $path);
$img = getRandomFromArray($imgList);
?>
<img src="/<?php echo $path . $img ?>" alt="image" />
You'll need to add this at the top of every page to get your image:
session_start();
if(isset($_SESSION['UserImg'])){
$img = $_SESSION['UserImg'];
}
else {
$img = getRandomFromArray($imgList);
$_SESSION['UserImg'] = $img;
}
This should work out!
on top of each page call:
session_start();
To safe variable in session for example:
$_SESSION['imageid'] = $ID
To get the variable back:
$imageid = $_SESSION['imageid']

Code for exif data of an image

Is there any code for getting all exif data of an image?
I want all possible data that can be extraxted from an image file(which the user will upload on my webpage) like bitrate ,file owner and security details. Also,if there have been any comments added to the image,i want to get that too.
If an API exists to do these things for any file(video,text,image) then it will be really helpful.
Thank you,
Shubham
you can do this with php
example:
<?php
echo "test1.jpg:<br />\n";
$exif = exif_read_data('tests/test1.jpg', 'IFD0');
echo $exif===false ? "No header data found.<br />\n" : "Image contains headers<br />\n";
$exif = exif_read_data('tests/test2.jpg', 0, true);
echo "test2.jpg:<br />\n";
foreach ($exif as $key => $section) {
foreach ($section as $name => $val) {
echo "$key.$name: $val<br />\n";
}
}
?>
or there is a exif.js that will do it aswell

File upload in joomla module

I have been searching this for quite a while but couldn't find a solution to match my need. I am developing a module for Joomla 2.5 . I need functionality to allow users to upload images/any file type from the backend in module configuration.
Question : How can I add field for file upload in joomla module.
Thanks!
Just a sample from the joomla docs:
<?php
//Retrieve file details from uploaded file, sent from upload form
$file = JRequest::getVar('file_upload', null, 'files', 'array');
//Import filesystem libraries. Perhaps not necessary, but does not hurt
jimport('joomla.filesystem.file');
//Clean up filename to get rid of strange characters like spaces etc
$filename = JFile::makeSafe($file['name']);
//Set up the source and destination of the file
$src = $file['tmp_name'];
$dest = JPATH_COMPONENT . DS . "uploads" . DS . $filename;
//First check if the file has the right extension, we need jpg only
if ( strtolower(JFile::getExt($filename) ) == 'jpg') {
if ( JFile::upload($src, $dest) ) {
header("Location: http://".$_SERVER["HTTP_HOST"]."/administrator"); Redirect to a page of your choice
} else {
echo "error !"; //Redirect and throw an error message
}
} else {
echo "Wrong extension !"; //Redirect and notify user file is not right extension
}
?>
For more detailed information and full example(s): http://docs.joomla.org/How_to_use_the_filesystem_package
Keep in mind that your HTML form must include
enctype="multipart/form-data"
otherwise you will not get any result with the joomla function!

Resources