2011-03-18 67 views
0

所以基本上我有一个网站,允许某些成员上传图像(漫画页)到他们自己的图像库(到特定的漫画)。我有一个成功的图片上传脚本,我用它来上传每个成员的个人资料/头像图片,但现在我想将文件上传到更具体的地方,我遇到了一些麻烦。用户上传图像到特定的目录

这是我到目前为止有:

(这是在页面的顶部出现的内容)

<?php 
session_start(); 
$toplinks = ""; 
if (isset($_SESSION['id'])) { 
    // Put stored session variables into local php variable 
    $userid = $_SESSION['id']; 
    $username = $_SESSION['username']; 
    $toplinks = '<a href="member_profile.php?id=' . $userid . '">' . $username . '</a> &bull; 
    <a href="member_account.php">Account</a> &bull; 
    <a href="logout.php">Log Out</a>'; 
} else { 
    $toplinks = '<a href="join_form.php">Register</a> &bull; <a href="login.php">Login</a>'; 
} 
?> 

(这是上传脚本)

<?php 
// Here we run a login check 
if (!isset($_SESSION['id'])) { 
    echo 'Please <a href="login.php">log in</a> to access your account'; 
    exit(); 
} 
// Place Session variable 'id' into local variable 
$id = $_SESSION['id']; 
// Process the form if it is submitted 
if ($_FILES['uploadedfile']['tmp_name'] != "") { 
    // Run error handling on the file 
    // Set Max file size limit to somewhere around 120kb 
    $maxfilesize = 400000; 
    // Check file size, if too large exit and tell them why 
    if($_FILES['uploadedfile']['size'] > $maxfilesize) { 
     echo "<br /><br />Your image was too large. Must be 400kb or less, please<br /><br /> 
     <a href=\"upload_comic.php\">click here</a> to try again"; 
     unlink($_FILES['uploadedfile']['tmp_name']); 
     exit(); 
    // Check file extension to see if it is .jpg or .gif, if not exit and tell them why 
    } else if (!preg_match("/\.(gif|jpg|png)$/i", $_FILES['uploadedfile']['name'])) { 
     echo "<br /><br />Your image was not .gif, .jpg, or .png and it must be one of those three formats.<br /> 
     <a href=\"upload_comic.php\">click here</a> to try again"; 
     unlink($_FILES['uploadedfile']['tmp_name']); 
     exit(); 
     // If no errors on the file process it and upload to server 
    } else { 
     // Rename the pic 
     $newname = ""; //numbers only, so they show up sequentially 
     // Set the direntory for where to upload it, use the member id to hit their folder 
     // Upload the file 
     if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], "comics/$comicid/".$newname)) { 
      echo "Success, the image has been uploaded and will display to visitors!<br /><br /> 
      <a href=\"member_account.php\">Click here</a> to return to your profile edit area"; 
      exit(); 
     } else { 
      echo "There was an error uploading the file, please try again. If it continually fails, contact us by email. <br /><br /> 
      <a href=\"member_account.php\">Click here</a> to return to your profile edit area"; 
      exit(); 
     } 
    } // close else after file error checks 
} // close if post the form 
?> 

理想的情况下,我希望能够上传这样的图像:comics/comic_id/chapter_id/uploaded_file.extension

通过用户配置文件图片上传器,我可以从$ _Session ['id']变量中获取$ ID,但是对于漫画,我不知道如何获取这些信息并使用它来设置comic_id目录(chapter_id将在表单中被选中,所以我不太担心那个)。

有什么想法?

+0

我不确定你在问什么。我想你想看看他们的文件夹中有多少个顺序编号的文件,按顺序给它下一个ID?您还应该让他们选择覆盖现有文件,不是吗? – 2011-03-18 20:16:52

+0

是的。虽然我并不太担心这一部分,但我更关心如何让我的目录路径有序。这可能不难,我只是不知道如何去做。 – TalesStudio 2011-03-18 20:25:20

回答

0

您可以上传文件到您选择的任何地方。这会将漫画保存在其ID和章节的文件夹中,但保留文件名。如果你想使用漫画ID作为文件名,我相信你可以解决这个问题。

$basepath = "/home/path/to/www/comics/member_" . $member_id . "/"; 

function construct_path($chapter_id,$comic_id) 
{ 
    $saveimagepath = $basepath . $comic_id . $chapter 
} 

if (!isset($_SESSION['id'])) { 
    echo 'Please <a href="login.php">log in</a> to access your account'; 
    exit(); 
} 
// Place Session variable 'id' into local variable 
$id = $_SESSION['id']; 
// Process the form if it is submitted 
if ($_FILES['uploadedfile']['tmp_name'] != "") { 
    // Run error handling on the file 
    // Set Max file size limit to somewhere around 120kb 
    $maxfilesize = 400000; 
    // Check file size, if too large exit and tell them why 
    if($_FILES['uploadedfile']['size'] > $maxfilesize) { 
     echo "<br /><br />Your image was too large. Must be 400kb or less, please<br /><br /> 
     <a href=\"upload_comic.php\">click here</a> to try again"; 
     unlink($_FILES['uploadedfile']['tmp_name']); 
     exit(); 
    // Check file extension to see if it is .jpg or .gif, if not exit and tell them why 
    } else if (!preg_match("/\.(gif|jpg|png)$/i", $_FILES['uploadedfile']['name'])) { 
     echo "<br /><br />Your image was not .gif, .jpg, or .png and it must be one of those three formats.<br /> 
     <a href=\"upload_comic.php\">click here</a> to try again"; 
     unlink($_FILES['uploadedfile']['tmp_name']); 
     exit(); 
     // If no errors on the file process it and upload to server 
    } else { 
     // Rename the pic 
     $newname = $saveimagepath . $_FILES['uploadedfile']['tmp_name']; 
//numbers only, so they show up sequentially 
     // Set the direntory for where to upload it, use the member id to hit their folder 
     // Upload the file 
     if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $newname)) { 
      echo "Success, the image has been uploaded and will display to visitors!<br /><br /> 
      <a href=\"member_account.php\">Click here</a> to return to your profile edit area"; 
      exit(); 
     } else { 
      echo "There was an error uploading the file, please try again. If it continually fails, contact us by email. <br /><br /> 
      <a href=\"member_account.php\">Click here</a> to return to your profile edit area"; 
      exit(); 
     } 
    } // close else after file error checks 
} // close if post the form 
?> 
0

$ _SESSION变量可在任何使用session_start()启动的站点上使用。因此,如果在登录后立即设置了id,则可以使用$ _SESSION ['id']以相同的方式在任何其他页面访问此值。确保id的值不会杀死你的文件系统或导致安全问题!

+0

对不起,如果这听起来无知(php的概念化对我来说很难)。如果我从漫画列表中调用$ _Session ['id'](是由它们的id#标识的),它是否会将$ _SESSION ['id']设置为漫画ID?如果我已经使用$ _SESSION ['id']来获取用户ID,是否会造成问题?我在我的帖子中增加了一些PHP,我在页面的开头调用以检查登录。谢谢! – TalesStudio 2011-03-18 20:11:27

相关问题