Jump to content

upload pictures and display


blt4424

Recommended Posts

I need some help working through this. I have an uploader for files, but I'd like it to create a link of the file uploaded and automatically post it to a page. Each user has a profile page and I'd like these pictures to be posted to their profile page. I guess with a session, it would take the logged in username and post the pictures uploaded on the page " member_profile2.php?username=xxxx "

 

Here is my upload file for reference.

uploads.php

        <?php
            $Dir = "temporary";

            
            if (isset($_POST['upload'])) {

            		//if file uploaded
                if (isset($_FILES['new_file'])) {

                    //if successful
                    if (move_uploaded_file($_FILES['new_file']['tmp_name'], $Dir . "/" . $_FILES['new_file']['name']) == TRUE){
                        chmod($Dir . "/" . $_FILES['new_file']['name'], 0644);
                        echo "<b>File \"" . htmlentities($_FILES['new_file']['name']) ." was successfully uploaded </b><br />\n";

                        //displays file info
                        echo "Name: ". htmlentities($_FILES['new_file']['name']) . "<br />".
			"Size: " . $_FILES['new_file']['size'] . " bytes" . "<br />".
                             "Type: " . $_FILES['new_file']['type'];
                    }
                    //if not successful
                    else {
                        echo "<b>Uploading \"" . htmlentities($_FILES['new_file']['name']) . " was unsuccessful</b><br />\n";
                    } 
                }  
            }  
        ?>

        <form action="uploads.php" method="POST" enctype="multipart/form-data">
            <input type="hidden" name="MAX_FILE_SIZE" value="250000" /><br />
            Select file to upload:<br />
            <input type="file" name="new_file" />
            250kB limit!<br />
            <input type="submit" name="upload" value="Upload File" /><br />         
        </form>

?>

 

 

and my member_profile2.php

<?php /*?><?php error_reporting(E_ALL ^ E_NOTICE); ?><?php */?>
<?php
# Starting the session
session_start();

# Requiring SQL connection
require_once 'mysql-connect2.php';

# Setting auser as SESSION['user']
$auser = $_SESSION['user'];

# SQL protecting variables
$username = (isset($_GET['username']))?mysql_real_escape_string($_GET['username']):$username;

# Checking through each query
if(isset($auser)) {
  $sql = mysql_query("SELECT * FROM `user` WHERE `username` = '$username'");
  if(mysql_num_rows($sql)) {
    while($row = mysql_fetch_array($sql)) {
      $page = "<h1>User Info</h1>".
              "<b>Username: {$row['username']}<br /><br />".
		  "<b>First Name: {$row['firstname']}<br /><br />".
		  "<b>Last Name: {$row['lastname']}<br /><br />".
		  "<b>Email: {$row['email']}<br /><br />".
              "<form name=\"backlistfrm\" method=\"post\" action=\"members2.php\">".
              "  <input type=\"submit\" value=\"Back to The List\">".
              "</form><br />";
		      }
  } else {
    $page = "ERROR: No member found for username: <strong>{$_GET['username']}</strong>.";
  }
} else {
  $page = "ERROR: Not logged in.";
}

# Printing the final output
print $page;

?>

 

I'd like the pictures to be displayed after the user details.

Any help with this would be great.

 

 

I was also thinking, could I just have the uploader save it as a filename with the user's username? For example, "user1-image1.jpg". And then have their profile page pull and display any files with the name user1-image1-10.jpg (10 being max possible uploads for that user).

 

How would I do this? This sounds maybe easier.

Link to comment
https://forums.phpfreaks.com/topic/220781-upload-pictures-and-display/
Share on other sites

Still working on this...I'm trying to rename my upload to the username of the logged in user.

 

I'm trying to rename a file uploaded to the username of the logged in user. I can't seem to figure it out. I'm trying to mimic how I got the username and details on my profile pages. The pictures are uploaded to a folder, not a database.

 

Here's my uploads.php

     <?php session_start();
include 'mysql-connect2.php';

# Setting auser as SESSION['user']
$auser = $_SESSION['user'];

# SQL protecting variables
$username = (isset($_GET['username']))?mysql_real_escape_string($_GET['username']):$username;

if(isset($auser)) {
  $sql = mysql_query("SELECT * FROM `user` WHERE `username` = '$username'");
// directory path.


// get the dir to send file to and the file name.

$uploadfile = $username . basename($_FILES['userfile']['name']);

// get the current file information.
$file=$uploadfile;

//get the . and file exstention.
$ext = substr($file, -4);

//convert varable to the uplaoded directory the new id
//and extention.

$uploadfile=$sql.$ext;

//rename the file to the new one.

@rename($file,$uploadfile);

// if all the conditions are correct send the file to the directory.

if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)){

// success echoed

echo " <font color='red'>File is valid, and was successfully uploaded.</font>";

}else {

//unsuccesfull echoed

echo "<font color='red'>File was unsuccesful sorry</font>";

}
}
// show the form.
   
echo"
<form enctype='multipart/form-data' action='uploads.php' method='POST'>
   
<input type='hidden' name='MAX_FILE_SIZE' value='30000000000000000000000'>

send this file <input name='userfile' type='file' >
    
<input type='submit' name='submit' value='Send File'>

</form>";

?>

 

 

 

 

And my profile page code for reference.

<?php /*?><?php error_reporting(E_ALL ^ E_NOTICE); ?><?php */?>
<?php
# Starting the session
session_start();

# Requiring SQL connection
require_once 'mysql-connect2.php';

# Setting auser as SESSION['user']
$auser = $_SESSION['user'];

# SQL protecting variables
$username = (isset($_GET['username']))?mysql_real_escape_string($_GET['username']):$username;

# Checking through each query
if(isset($auser)) {
  $sql = mysql_query("SELECT * FROM `user` WHERE `username` = '$username'");
  if(mysql_num_rows($sql)) {
    while($row = mysql_fetch_array($sql)) {
      $page = "<h1>User Info</h1>".
              "<b>Username: {$row['username']}<br /><br />".
		  "<b>First Name: {$row['firstname']}<br /><br />".
		  "<b>Last Name: {$row['lastname']}<br /><br />".
		  "<b>Email: {$row['email']}<br /><br />".
              "<form name=\"backlistfrm\" method=\"post\" action=\"members2.php\">".
              "  <input type=\"submit\" value=\"Back to The List\">".
              "</form><br />";
		      }
  } else {
    $page = "ERROR: No member found for username: <strong>{$_GET['username']}</strong>.";
  }
} else {
  $page = "ERROR: Not logged in.";
}

# Printing the final output
print $page;

?>

 

Edit: sorry had wrong code.

 

I'm getting resource id #5 on the file name save instead of the username?? I really need help with this!! I'm a noob to php. Thanks in advance.

 

Need help getting this to work!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.