General [M]ayhem

Go Back   General [M]ayhem > Real Time Sub-Forums > CompuGlobalHyperMegaNet
Register Members List Mark Forums Read [M]erchandise Calendar

Reply
 
Thread Tools
Blt44
 
Need PHP help. I'm a noob ITT

I'm trying to rename image files that are uploaded to the name of the user uploading them by using a session.

aka image1.jpg becomes user1.jpg.

However, it's renaming it to resource id # 5.jpg.


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

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


$username = (isset($_GET['username']))?mysql_real_escape_string($_GET['username']):$username;

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


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


$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>";

?>

Halp plz?
__________________
http://thedailyshens.blogspot.com/
Old 12-05-2010, 08:13 PM Blt44 is offline  
Reply With Quote
#1  

Advertisement [Remove Advertisement]

MooK
 
MooK's Avatar
 
First thing's first:

http://www.tizag.com/phpT/postget.php

You're posting the form data, but trying to $_GET it. I sense KpaBap informing me at a later date that there's more to it.

Also, your code is kind of confusing. Why aren't you testing for submission before everything else?

Code:
// Only the cool people use ternary operators to save three lines of code.
$username = (!$_SESSION['user']) ? $_POST['username'] : $_SESSION['user'];

// If you're not allowed, you're not allowed.
if (!$username) {
     print("This is a local store!");
     exit;
}
__________________
MooK - <3 Website Architecture.
Synth3t1c owes me $20 via PayPal.
Old 12-05-2010, 08:42 PM MooK is offline  
Reply With Quote
#2  

Blt44
 
Cause I'm a tard.

I've fixed it now Much thanks.

Also, while I have you, how can I make it generate an incrementing number after the username? So userA1.jpg, userA2.jpg, etc.

And, one last question, how could I display say userA1.jpg thru userA10.jpg?


__________________
http://thedailyshens.blogspot.com/
Old 12-05-2010, 08:50 PM Blt44 is offline  
Reply With Quote
#3  

MooK
 
MooK's Avatar
 
Quote:
Originally Posted by Blt44 View Post
Cause I'm a tard.

I've fixed it now Much thanks.

Also, while I have you, how can I make it generate an incrementing number after the username? So userA1.jpg, userA2.jpg, etc.

And, one last question, how could I display say userA1.jpg thru userA10.jpg?



I'm going to assume you want the application to identify how many files belong to a user, and determine, from there, how to increment beyond the last file uploaded. You can probably get away with readdir and some regex. http://php.net/manual/en/function.readdir.php

Basically, you use readdir to compile all of the filenames into an array, and check the array for files that match a certain username, using regex. Then you can find the last file added and set that number, plus one, to the file being uploaded. This is based upon my limited understanding of your setup.

You can eliminate a lot of the regex by using specific folders for usernames. This would be a much better solution:

public/images/username/1.gif
public/images/username/2.gif

Using this method, technically, the number of files in the folder will always reflect the number the images were last at. Say there's 30 images in the folder, then username1-username30 is already present. Add one, append the file and upload.

Then again, if you're using specific folders, you really don't need to number images and assign them to a user unless it's absolutely necessary for another reason.


Multiple files will require the increment operator (++) so there's no overwriting.


Just create the folder when you create the user.
__________________
MooK - <3 Website Architecture.
Synth3t1c owes me $20 via PayPal.

Last edited by MooK; 12-05-2010 at 09:10 PM..
Old 12-05-2010, 09:03 PM MooK is offline  
Reply With Quote
#4  

Blt44
 
Yeah I've just made it create a user folder upon registering.

Is there an easy way to display thumbnails?

Here's how I'm showing the images in the user's folder.

Code:
$dirname = "./$username/";
$images = scandir($dirname);
$ignore = Array(".", "..");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
echo "<img src='./$username/$curimg' /><br>\n";
};
}
__________________
http://thedailyshens.blogspot.com/
Old 12-05-2010, 09:56 PM Blt44 is offline  
Reply With Quote
#5  

MooK
 
MooK's Avatar
 
There are numerous ways to do it, however, I'm also going to assume you don't have thumbnails currently created.

You can go the "easy" way, which I wouldn't recommend, and add a CSS class to modify the image size directly.

Another solution would be to make use of something like phpThumb (http://phpthumb.sourceforge.net/) which is almost self-explanatory (you will find lots of tutorials for this) and use that to create a thumbnail.
__________________
MooK - <3 Website Architecture.
Synth3t1c owes me $20 via PayPal.
Old 12-06-2010, 05:23 AM MooK is offline  
Reply With Quote
#6  

Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -7. The time now is 04:47 PM.



Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.