javascript - image retrieval from directory and session variable issues - php - Stack Overflow

I have a problem when retrieving the images from a directory on my server, so what the main sequence is

I have a problem when retrieving the images from a directory on my server, so what the main sequence is: in a page (multiupload.php) I added the input, allowed the image to be previewed and when the user submitted, a new directory with their session id would be created, the images would then be stored in the unique directory and the page would then be directed to (drag.php). The newly loaded page has a canvas with different divs to controls filters that are attached to that canvas. My problem lies with retrieving the image with the specified s_id as a directory name from one page to the other.

Q: Am i retrieving session variables properly? or using them appropriately?

This is the necassary snippets from multiupload.php's upload script.

<?php
  $dir_id = session_id(md5(uniqid()));
  session_start($dir_id);
  $path = "uploads/";
  $dir = $path.$dir_id;
  $path = $path.$dir_id."/";
  if (file_exists($dir)) {
    system('/bin/rm -rf ' . escapeshellarg($dir));
  } else {
    mkdir($path);
    chmod($path, 0722);  
  }
  $_SESSION["id"] = $dir_id;
  $_SESSION["directory"] = "/" . $dir;
  $_SESSION["path_name"] = $path;
?>

I define the directory, whole path and the id for the directory. I would like to retrieve the id in the next page, but it's not doing it correctly.

and this is the retrieval code from drag.php

$realPath = 'uploads/'. echo $_SESSION['id'];

$handle = opendir(dirname(realpath(__FILE__)).$realPath;
while($file = readdir($handle)){
    if($file !== '.' && $file !== '..'){
        echo '<img src="uploads/'.$file.'" border="0" />';
    }
}

My end result is that I would like all images to be drawn on the page. For now I would like them to be drawn anywhere aslong as they're visible.

If my question isn't clear, feel free to edit or ment where I should change. If you need more code or information, please let me know.

I have a problem when retrieving the images from a directory on my server, so what the main sequence is: in a page (multiupload.php) I added the input, allowed the image to be previewed and when the user submitted, a new directory with their session id would be created, the images would then be stored in the unique directory and the page would then be directed to (drag.php). The newly loaded page has a canvas with different divs to controls filters that are attached to that canvas. My problem lies with retrieving the image with the specified s_id as a directory name from one page to the other.

Q: Am i retrieving session variables properly? or using them appropriately?

This is the necassary snippets from multiupload.php's upload script.

<?php
  $dir_id = session_id(md5(uniqid()));
  session_start($dir_id);
  $path = "uploads/";
  $dir = $path.$dir_id;
  $path = $path.$dir_id."/";
  if (file_exists($dir)) {
    system('/bin/rm -rf ' . escapeshellarg($dir));
  } else {
    mkdir($path);
    chmod($path, 0722);  
  }
  $_SESSION["id"] = $dir_id;
  $_SESSION["directory"] = "/" . $dir;
  $_SESSION["path_name"] = $path;
?>

I define the directory, whole path and the id for the directory. I would like to retrieve the id in the next page, but it's not doing it correctly.

and this is the retrieval code from drag.php

$realPath = 'uploads/'. echo $_SESSION['id'];

$handle = opendir(dirname(realpath(__FILE__)).$realPath;
while($file = readdir($handle)){
    if($file !== '.' && $file !== '..'){
        echo '<img src="uploads/'.$file.'" border="0" />';
    }
}

My end result is that I would like all images to be drawn on the page. For now I would like them to be drawn anywhere aslong as they're visible.

If my question isn't clear, feel free to edit or ment where I should change. If you need more code or information, please let me know.

Share Improve this question edited Apr 6, 2015 at 12:04 Legendary 2,24218 silver badges26 bronze badges asked Apr 1, 2015 at 15:10 BradleyIWBradleyIW 1,3583 gold badges20 silver badges37 bronze badges 2
  • 3 Use this line $realPath = 'uploads/'.$_SESSION['id']; instead of $realPath = 'uploads/'. echo $_SESSION['id']; – Apoorv Bambarde Commented Apr 6, 2015 at 11:54
  • Good interprettor @ApoorvBambarde . – Asif Mehmood Commented Apr 6, 2015 at 11:56
Add a ment  | 

4 Answers 4

Reset to default 6 +50

Please modify your code to this code:

<?php
  $dir=$_SESSION['id'];
  $realPath = '/uploads/'.$dir;
  $handle = opendir(dirname(realpath(__FILE__)).$realPath);
    while($file = readdir($handle)){
      if($file !== '.' && $file !== '..'){
        echo '<img src="'.$realPath.'/'.$file.'" border="0" width="200" />';
    }
  }
?>

I have use this code an I get the o/p like this:

<?php
  $dir_id = session_id(md5(uniqid()));
  session_start();
  $path = "uploads/";
  $dir = $path.$dir_id;
  $path = $path.$dir_id."/";
  if (file_exists($dir)) {
      system('/bin/rm -rf ' . escapeshellarg($dir));
  } else {
    mkdir($path);
    chmod($path, 0722);  
  }
  $_SESSION["id"] = $dir_id;
  $_SESSION["directory"] = "/" . $dir;
  $_SESSION["path_name"] = $path;
?>

In any file.php, which u need get session:

<?php
  session_start();

  $realPath = 'uploads/'.$_SESSION['id'];

  $handle = opendir(dirname(realpath(__FILE__)).$realPath;
  while($file = readdir($handle)){
      if($file !== '.' && $file !== '..'){
          echo '<img src="uploads/'.$file.'" border="0" />';
      }
  }
?>

I advice to you read that: http://www.w3schools./php/php_sessions.asp when i was started with php 6 years ago - it was rly helpful

session_start does not take any argument. It's just to put a cookie and to read the session variables. (exposed in $_SESSION). You have to use session_start() on every pages to be able to read the $_SESSION variables.

this will give only image file from directory using session variable.

<?php
  $dir=$_SESSION['id'];
  $realPath = '/uploads/'.$dir;
  $handle = opendir(dirname(realpath(__FILE__)).$realPath);
    while($file = readdir($handle)){
      if($file !== '.' && $file !== '..'){
      if(fnmatch('*.jpg', $file) || fnmatch('*.png', $file) || fnmatch('*.jpeg', $file)){
            echo '<img src="'.$realPath.'/'.$file.'"/>';
         }
    }
  }
?>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745272685a4619844.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信