2015-04-01 56 views
3

从我的服务器上的目录中检索图像时出现问题,所以主要序列是什么:在页面(multiupload.php)中添加了输入,允许图像进行预览,当用户提交时,会创建一个带有会话ID的新目录,然后图像将存储在唯一目录中,然后页面将被定向到(drag.php)。新加载的页面具有包含不同div的画布,以控制附加到该画布的过滤器。我的问题在于检索具有指定s_id作为从一个页面到另一个目录名称的映像。从目录和会话变量问题检索图像-php

问:我是否正确检索会话变量?或适当地使用它们?

这是来自multiupload.php的上传脚本的necassary片段。

<?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; 
?> 

我为目录定义了目录,整个路径和id。我想在下一页中检索该id,但它没有正确执行。

,这是从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" />'; 
    } 
} 

我的最终结果是,我想所有的图像在页面上绘制。现在我希望他们能够在任何地方被吸引,只要他们是可见的。

如果我的问题不清楚,请随时编辑或评论我应该改变的地方。如果您需要更多的代码或信息,请告诉我。

+3

使用此行$ realPath ='uploads /'.$_ SESSION ['id'];而不是$ realPath ='uploads /'。 echo $ _SESSION ['id']; – 2015-04-06 11:54:47

+0

好解释器@ApoorvBambarde。 – 2015-04-06 11:56:58

回答

6

请修改您的代码,这段代码:

<?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" />'; 
    } 
    } 
?> 

我用这个代码,我得到像这样的O/P:

enter image description here

1

session_start不带任何参数。这只是放一个cookie并读取会话变量。 (暴露在$ _SESSION中)。您必须在每个页面上使用session_start()才能读取$ _SESSION变量。

2
<?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; 
?> 

在任何file.php,其中U需要会话中获取:

<?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" />'; 
     } 
    } 
?> 

我给你的建议阅读:http://www.w3schools.com/php/php_sessions.asp当我在使用PHP 6年前开始 - 这是有帮助的RLY

+0

我认为这更多的是指出你需要从任何你想访问会话数据的页面调用session_start().. – Mayhem 2015-04-06 11:54:09

+0

所以,我写了“在任何file.php中,你需要得到session:”,我是错误? – Legendary 2015-04-06 11:56:13

+0

这是正确的,它的常见做法是在每个页面的顶部包含/需要一个常见的php文件。像header.php,并且包含像session_start()这样的项目。你是对的,你需要在脚本中调用它之后才能访问它们 – Mayhem 2015-04-06 11:59:32

1

这将使用会话变量从目录中只给出了图像文件。

<?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.'"/>'; 
     } 
    } 
    } 
?>