Jump to content
xisto Community
Sign in to follow this  
cragllo

Help With Uploading Files!

Recommended Posts

Here is the code i am going to use, written originaly for only 2 files, image and location, I added 'thumbnail', can someone check through it please?

$submit = $_POST['submit'];$name = $_POST['name'];$description = $_POST['description'];$age = $_POST['age'];$subcat = $_POST['subcat'];$with = $_POST['with'];$added = $_POST['added'];$type = $_POST['type'];$location = $_POST['location'];$thumbnail = $_POST['thumbnail'];$image = $_POST['image'];$area = $_POST['area'];if($submit){//File Upload Scripterror_reporting(E_ALL);//Configuration//Specific File extensions (don't add the dot)$fileextension['image'][] = "gif";$fileextension['image'][] = "jpg";$fileextension['image'][] = "jpeg";$fileextension['image'][] = "png";$fileextension['thumbnail'][] = "gif";$fileextension['thumbnail'][] = "jpg";$fileextension['thumbnail'][] = "jpeg";$fileextension['thumbnail'][] = "png";$fileextension['file'][] = "zip";//Folders to place files in (No trailing slash) (For each different type in the list above, you must add an entry here.$filefolders['image'] = "/usr/local/psa/home/vhosts/universalsims.com/httpdocs/exchange/files/$area/clothing/";$filefolders['thumbnail'] = "/usr/local/psa/home/vhosts/universalsims.com/httpdocs/exchange/files/$area/clothing/";$filefolders['file'] = "/usr/local/psa/home/vhosts/universalsims.com/httpdocs/exchange/images/$area/clothing/";//Database connectivity settingsinclude("../config.php");//image$image['name'] = $_FILES['image']['name'];$image['tmpname'] = $_FILES['image']['tmp_name'];$tmpextension = explode(".", $image['name']);$image['extension'] = $tmpextension[1];$image['OK'] = FALSE;$image['folder'] = $filefolders['image'];//thumbnail$thumbnail['name'] = $_FILES['thumbnail']['name'];$thumbnail['tmpname'] = $_FILES['thumbnail']['tmp_name'];$tmpextension = explode(".", $thumbnail['name']);$thumbnail['extension'] = $tmpextension[1];$thumbnail['OK'] = FALSE;$thumbnail['folder'] = $filefolders['thumbnail'];//file$file['name'] = $_FILES['location']['name'];$file['tmpname'] = $_FILES['location']['tmp_name'];$tmpextension = explode(".", $file['name']);$file['extension'] = $tmpextension[1];$file['OK'] = FALSE;$file['folder'] = $filefolders['file'];//imageforeach ($fileextension['image'] as $value)        {        if ($image['extension'] == $value)                {                $image['OK'] = TRUE;                }        }//thumbnailforeach ($fileextension['thumbnail'] as $value)        {        if ($thumbnail['extension'] == $value)                {                $thumbnail['OK'] = TRUE;                }        }//fileforeach ($fileextension['file'] as $value)        {        if (!$value = 0)                {                if ($file['extension'] == $value)                        {                        $file['OK'] = TRUE;                        }                }        else                {                $file['OK'] = TRUE;                }        }//check themif (!$image['OK'] OR !$thumbnail['OK'] OR !$file['OK'])        {        echo "File Type for image or uploaded file not supported.";        die();        }//imageif (move_uploaded_file($image['tmpname'], $image['folder'] . $image['name']))        {        echo "Image uploaded successfully!";        }else        {        echo "<br /><br />Image could not be uploaded!";        die();        }//thumbnailif (move_uploaded_file($thumbnail['tmpname'], $thumbnail['folder'] . $thumbnail['name']))        {        echo "Thumbnail uploaded successfully!";        }else        {        echo "<br /><br />Thumbnail could not be uploaded!";        die();        }//fileif (move_uploaded_file($file['tmpname'], $file['folder'] . $file['name']))        {        echo "File uploaded successfully!";        }else        {        echo "<br /><br />File could not be uploaded!";        die();        }//add themmysql_query ("INSERT INTO downloads (name,description,age,subcat,with,added,type,location,thumbnail,image,area) VALUES ('$name','$description','$age','$subcat','$with','$added','$type','$location','$thumbnail','$image','area') ") or die (mysql_error());}

Also, I need the script the change the name of the files uploaded, they need to be changed to the id of the row in the database, as that has not been set yet, how can this be done?

Needs to be:
image: ID_1.jpg
thumbnail: ID_2.jpg
file: ID.zip

This is quite urgent, and i have no idea what to do,

Thanks,
Craig.

Share this post


Link to post
Share on other sites

Try this to take unique ID for file.
You need a field named ID type autoincrement

$query = "SELECT `ID` FROM `Table` ORDER BY `ID` DESC LIMIT 1";$result = mysql_query($query);if($result){  $line = mysql_fetch_array($result, MYSQL_ASSOC);   $UNIQUE_ID = .$line["ID"] + 1;  $image_name = $image."_".$UNIQUE_ID;  $file_name = $file."_".$UNIQUE_ID;etc.}

This work if you never allow user delete last enter to database. :unsure: Or you can not use autoincrement just only simple int fieltd. But this is can be risky.

Share this post


Link to post
Share on other sites

hm.... will that rename the files? and insert the new data into the database?...

I was shown this:

$newnames2 = mysql_query("SELECT * FROM downloads ORDER BY id DESC LIMIT 1;");while($newnames = mysql_fetch_array($newnames2)){$downloadid = $newnames[id];$newlocation = $newnames[location];$newthumbnail = $newnames[thumbnail];$newimage = $newnames[image];}rename("../files/$area/$newlocation.zip", "../files/$area/newfile.zip");rename("../images/$area/$newthumbnail.jpg", "../images/$area/$downloadid_1.jpg");rename("../images/$area/$newimage.jpg", "../images/$area/$downloadid_2.jpg");mysql_query ("UPDATE downloads(location,thumbnail,image) VALUES('$newlocation','$newthumbnail','$newimage') WHERE id='$downloadid'") or die (mysql_error());

Tell me if thats wrong...

Share this post


Link to post
Share on other sites

if you prefer change name instead insert changed try this:


// Table build1. ID integer autoincrement index2. IsProcessEnd integer(1) //put there 0 on first upload3. FileName string4. ThumbnailName string5. ImageName string<?php//consts$area = "../sitexxxfiles/";$filespath = $area."files/";$thumbnailpath = $area."thumbnailpath/";$imagespath = $area."images/";$result = mysql_query("SELECT * FROM downloads ORDER BY id WHERE IsProcessEnd = 0;");while($line = mysql_fetch_array($result, MYSQL_ASSOC)){  $NewFileName = "f_".$line["ID"].".zip";  $NewThumbnailName = "t_".$line["ID"].".jpg";  $NewImageName = "i_".$line["ID"]."jpg";  $isOK = 1;  if (rename ($filespath.$line["FileName"], $filespath.$NewFileName)) else {$isOK = 0;}  if (rename ($thumbnailpath.$line["ThumbnailName"], $thumbnailpath.$NewThumbnailName )) else {$isOK = 0;}  if (rename ($imagespath.$line["ImageName"] ,$imagespath.$NewImageName )) else {$isOK = 0;}  if ($isOK == 1)  {    mysql_query ("UPDATE downloads(IsProcessEnd, FileName,ThumbnailName,ImageName) VALUES('1', '".$NewFileName."', '".$NewThumbnailName."', '".$NewImageName."' ) WHERE id='".$line["ID"]."'") or die (mysql_error());  }}?>

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.