Jump to content

Recommended Posts

I am new to coding so please bare with me. I originally had my members edit_pic page to only upload one pic and overwrite the old pic if uploading a new one. I want my members to be able to upload multiple pics and have them saved to the membersFile/"Thier ID"/ directory on my server. After reading the tutorial from w3schools I decided to copy and paste their code, then change what needed to be changed. Here is the code:

 

<?php

session_start();

 

 

if (!isset($_SESSION['id'])) {

  echo 'Please <a href="login.php">log in</a> to access your account';

  exit();

}

 

$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 3mb

    $maxfilesize = 25165824;

    // 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 3mb or less, please<br /><br />

        <a href=\"edit_pic.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)$/i", $_FILES['uploadedfile']['name'] ) ) {

        echo "<br /><br />Your image was not .gif or .jpg and it must be one of those two formats, please<br />

        <a href=\"edit_pic.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

        function findexts ($filename) { $filename = strtolower($filename) ; $exts = split("[/\\.]", $filename) ; $n = count($exts)-1; $exts = $exts[$n]; return $exts; }

 

//This applies the function to our file 

$ext = findexts ($_FILES['uploaded']['name']) ;

 

 

$ran = rand () ;

$ran2 = $ran.".";

$target = "memberFiles/$id/";

$target = $target . $ran2.$ext;

// 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']['tmp_name'], $target)) {

            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 continues to happen please contact support. <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

?>

 

 

Now, when I test this page it will always echo the error. Can anyone please tell me what I did wrong and help me fix it?

Thanks for the fast reply and also, thanks for the info on W3school... After searching the net for hours... they were the only source I found that had information I was looking for without downloading software to do it for me. I would rather know how to do it. The error message I continue to get is : "There was an error uploading the file, please try again. If it continues to happen please contact support." Not sure why.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.