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 / combic_id / chapter_id/uploaded_file.extensionのような画像をアップロードできるようにしたいと思います。

ユーザープロファイル画像アップローダーを使用すると、$ _ Session['id']変数から$IDを取得できましたが、コミックでは、その情報を取得して、comic_idディレクトリを設定する方法がわかりません。 (chapter_idがフォームで選択されるので、あまり心配していません)。

何かご意見は?

4

2 に答える 2

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
?>
于 2011-03-18T21:25:30.120 に答える
0

$ _SESSION変数は、session_start()で開始したすべてのサイトで使用できます。したがって、ログインの直後にIDが設定されている場合は、$ _ SESSION ['id']を使用して、他のページで同じ方法でこの値にアクセスできます。idの値がファイルシステムを強制終了したり、セキュリティの問題を引き起こしたりしないことを確認してください。

于 2011-03-18T19:48:26.483 に答える