Jul 7, 2008

Help With Uploading Files!

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > PHP Programming

free web hosting

Help With Uploading Files!

cragllo
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?
CODE
$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 Script
error_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 settings
include("../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'];

//image
foreach ($fileextension['image'] as $value)
       {
       if ($image['extension'] == $value)
               {
               $image['OK'] = TRUE;
               }
       }

//thumbnail
foreach ($fileextension['thumbnail'] as $value)
       {
       if ($thumbnail['extension'] == $value)
               {
               $thumbnail['OK'] = TRUE;
               }
       }

//file
foreach ($fileextension['file'] as $value)
       {
       if (!$value = 0)
               {
               if ($file['extension'] == $value)
                       {
                       $file['OK'] = TRUE;
                       }
               }
       else
               {
               $file['OK'] = TRUE;
               }
       }

//check them
if (!$image['OK'] OR !$thumbnail['OK'] OR !$file['OK'])
       {
       echo "File Type for image or uploaded file not supported.";
       die();
       }

//image
if (move_uploaded_file($image['tmpname'], $image['folder'] . $image['name']))
       {
       echo "Image uploaded successfully!";
       }
else
       {
       echo "<br /><br />Image could not be uploaded!";
       die();
       }

//thumbnail
if (move_uploaded_file($thumbnail['tmpname'], $thumbnail['folder'] . $thumbnail['name']))
       {
       echo "Thumbnail uploaded successfully!";
       }
else
       {
       echo "<br /><br />Thumbnail could not be uploaded!";
       die();
       }

//file
if (move_uploaded_file($file['tmpname'], $file['folder'] . $file['name']))
       {
       echo "File uploaded successfully!";
       }
else
       {
       echo "<br /><br />File could not be uploaded!";
       die();
       }

//add them
mysql_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.

 

 

 


Reply

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


$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. smile.gif Or you can not use autoincrement just only simple int fieltd. But this is can be risky.

Reply

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

I was shown this:
CODE
$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...

 

 

 


Reply

palladin
if you prefer change name instead insert changed try this:


CODE

// Table build

1. ID integer autoincrement index
2. IsProcessEnd integer(1) //put there 0 on first upload
3. FileName string
4. ThumbnailName string
5. 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());
 }
}
?>

Reply

cragllo
hmm, thats a better way to do it, but will that change the actual file name or jsut what is in the database?
EDIT: I see, that makes much more sense now, thanks!

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : uploading files

  1. Getting List Of Directories And Files Using Php - PHP Function for Directory and File List (6)
    is there a php function that lists the content of some folder.... example: /New folder new.txt
    left.gif download.zip dc.exe ....so is there..? /rolleyes.gif' border='0'
    style='vertical-align:middle' alt='rolleyes.gif' /> ...
  2. Need Some Help In File Browser - listing all sub folders and files in them. (8)
  3. Download Script For Mp3 Files - (0)
    Hello, I'm looking for a download script for sound files (e.g. mp3, avi, wma, and other ones).
    i have found a few download scripts but they would not work for sound files for some reason. also
    this will not be used for allowing downloading of illegal or riped music, what i will be using this
    script for is i'm making a site for my church and the pastor wants to be able to recored the
    services and then have me upload them to the site so that the church members can download them for
    what ever reason. If some one could tell me how to make one or could show me a plac...
  4. Change Permission With Php Code - code to change files' and folders' permissions? (3)
    As everyone know, there two ways (that I can think of) to change files' and directories'
    permissions. One is to change it in your cPanel's Disk Manager and the other is with an FTP
    client that supports chmod. Well, I'm doing something for my site that requires files to have
    full permissions (Execute, Write, and Read on all three groups). At first, I thought that if I made
    the directory 777, then every file created in that directory will be 777 as well. I'm wrong. An
    alternative to doing this is to change each file permission myself, but that would be...
  5. Need Help Uploading Php - (8)
    where can i upload php file and use it? im using this free webs the trap17 i want to use php like
    a website...
  6. Grabt Access To My Protected Files - grabt access to my protected files (2)
    Hi all, I am sure all of you are great programers but First i am no code programmer i am just
    trying to learn to run my sites, will the problem is i got a code from the web to protect my files
    ,1st i want to know how to work it out then i will try other things but can't let it go without
    a fight thanx all php: CODE <?php if ( ! defined( 'myname' )
    ) {         print "You cannot access this file directly.";         exit(); }
    customer data here ?> now i can't access my files what is the meth...
  7. Security Issue Writing Files - Security issue writing files (1)
    Hi, first, sorry about my english. i am a beginner with php and i have some question about writing
    files using php in a shared hosting. is a risk?, use database to store data is a better way? i just
    want make an interface (in php) that write the data in a .html extension file to show to everybody
    the html page and just the php interface is to the content manager. thanks in advance ...
  8. [php]simple Flat File Text Manipulator - Example on how to use forms to write to files in PHP (3)
    I made a simple flat file text editor, that can show you probably how simple it is to use forms with
    php and write that data to file. This example has 2 files, submit.php, and postit.html. Submit.php
    is used to write title, and some text, and add html tags, and paragraph tags where new paragraphs
    are. Here's the file with comments. I think that HTML really doesn't need some more
    explaining. CODE Title: <br /> <input type="text"
    name="title" size="53"> <br /> Text: <br />
    <textarea nam...
  9. Forms, Text Files, And Php For A Signature Generator. - Help a little. (1)
    Hello everyone! I am in need of some code a for a signature generator I am making. I am using
    BuffaloHELP's code for the php file, now I am trying to improve that code by making a form in a
    html file that will have the user say what is on the sig! But now, I need help getting the form
    data that is posted by the user to get into that sig! There is a file, sig.txt, where that tells
    the php file what text will go on the sig. But how can I make the form data in the html file go into
    the text file so it will go onto the sig? You might want to read BuffaloHELP&...
  10. Reading Files From Directory To Array, And Using $_get To Get Them - Simple way to manage lot's of files (2)
    Some user posted a similar problem i had when i tried to figure out how to update content on my
    website in less work as possible. This is just part of the "big" plan i have for my site but it can
    be helpfull to you guys if you like FlatFile. I hope that mod's don't mind me posting the
    same code here and on their forums, couse if they do i'll delete it from their forums
    /biggrin.gif" style="vertical-align:middle" emoid=":D" border="0" alt="biggrin.gif" /> So
    here's the situation. Let's say you have a folder called 'myfilesdirectory' on your
    s...
  11. Uploading A Photo... - (3)
    hi everyones


    I'm trying to have a photo gallery. I want to have the possibility to upload a photo. Looking on
    the web I tryed out to do so with php. But it is not working. I get this error...
    ...
  12. Writing To Files And Such - (3)
    Ok lets say i have a config.php file, i want to edit something without open it, i know every line
    number and that, now how would i do this? I will show you a example: CODE <?php
    $CONFIG['user']            =    'root';
    $CONFIG['server']        =    'localhost';
    $CONFIG['pass']            =    '******';
    $CONFIG['db']            =    '******';
    $CONFIG['install']        =    '1';
    $CONFIG['lang']       ...
  13. Files Required? - (5)
    I want to learn php and I am already on the first steps to doing it. I have set up my server using
    easyPHP as well as XAMPP which is a package containing FileZilla, Apache and MySQL. Other than
    easyPHP, XAMPP and MySQL, what other softwares are required to be set up for me to successfully
    learn how to program with PHP?...
  14. How To Sort Files Of A Directory using Php - (11)
    The following code displays the files of folder...but they are displaced by the order of adding... i
    want to sord the files / folders alphabetically and sord by accending order and by decending
    order.. can some one help me. $path = ""; $dir_handle = @opendir($path) or
    die("Unable to open $path"); echo "Directory Listing of $path "; while($file =
    readdir($dir_handle)) { if(is_dir($file)) { continue; } else
    if($file != '.' && $file != '..') { echo " $fi...
  15. Displaying Files Of A Directory - (2)
    I want to display the contents of a directory.. i have the following code.. It gives the output in
    one column only... like file1 file2 file3 file4 . . . . . Since there are lot of files so this
    column gets very long..i want to display the x number of files in each column.. like if there are 22
    files.. then file 1 file 11 file 21 file 2 . file 22 file 3 .. . . . file 10
    file 20 This was just an example..I know it can be done by using but i dont know how to do it
    with loop. Please help me. QUOTE $dir = './'; $handle =...
  16. Uploading From Url - any scripts? (9)
    Is there any scripts that allows user to upload files from a certain url to your server?...
  17. Logging Dowload Files From Your Server Onto A Html File - (1)
    Well, i had the idea of logging the downloads from my web in a html file few weeks ago, and i solved
    it with a lil php page included in my homepage. You could name the links with a name like
    "download.php?file=filename.ext" and then, in the download.php put the next code: (well you put
    the html and head and body tags if u want, i only write the php here) CODE <? if
    (isset($_GET['file']))
    $file=$_GET['file']; //so it gets the GET data from url
    (file=filename.ext); $ip=$_SERVER[...
  18. How To List Files In A Directory + Subdirectory And Then Use Them. - (8)
    So lets say i have folders called friends and work in a folder called pics. how would i make a
    function that lists the files in those folderscalled images kinda like this: CODE
    $directory = "./" function listfiles($directory) { //here should go the
    script to list the files in those directories. so that i can continue to work with them. like for
    example if there were images it would list all  the images and i could write a script to make a
    thumb of them and then save it into a thumb folder(not asking for all of that). but how w...
  19. Directory Files Displaying - (5)
    I have many files in a directory..I want to create a page , like A B C D E F ..... when some one
    click on A , it should display all the files starting from letter A , and when clicks on B , it
    should show all the files in that directory starting from B , and so on... I have no idea how to
    display the files of the directory iin that way. Kindly assist me.....
  20. Displaying Latest Added Files Of Subdirectories ? - (5)
    For example,i have a directory , which has 3 sub directories a,b and c...and i have some files in
    all subdirectories.. is it possible that i can display the latest added files to any of sub
    directory a,b, or c.?i think i will have to use sort by date function but how it should be done that
    it compares the files of all the subdirectories directories ?...
  21. What Kind Of Files Are .lib - (2)
    I want to know , if we use some pre-made php script , there are often many files with other
    extension..i have been watching many times some files names ".lib" . what kind of files these are
    ... there's written C/C++ inline file.. why these kinds of files are needed in php scripts.. and
    are there some replacements for it ? (So that we can do things without it ?)... for example im
    pasting the coding of a file.. ban.lib ..i want to ask, why it is .lib file..why it cant be .php....
    CODE <? function file2str($p){if($p=="") retur...
  22. Here's A Nice And Interesting Way To Make Comments - In your php files (9)
    We all know that when we make websites, sometimes, we just don't want a code to be there... for
    now. What I'm saying is, for example: Your name is Bob. Yes it's Bob. Bob made a website
    and added lots of content. He gets a lot of traffic. He signs up for Google Adsense! Yay! He
    is happy. He makes thousands of dollars a month. He is famous. And that is the exact reason why
    your enemy, Angela, is trying to make your google adsense account bad. Guess what? Angela is your
    sister. Yes she is your sister. What does that mean? She uses the same IP address...
  23. I Need Help With Wordpress/php - I am lost with these files! (7)
    Okay, here is the story. I set up wordpress on my site. I know nothing about PHP and need help. I
    need to know where the url and title info is stored. I checked in the header PHP file, and all I
    saw was: CODE <title><?php bloginfo('name'); wp_title();
    ?></title> And <div id="header"> <h1><a
    href="<?php bloginfo('url'); ?>"><?php
    bloginfo('name'); ?></a></h1> </div> <!--
    /header --> Whe...
  24. Help With Removing Files And Folders - removing all files from a directory (4)
    Is there a function or a group of functions which I can use to delete all the files an folders in a
    directory? I've tried rmdir() but it complains that it can't remove it because there are
    files/folders in it. ...
  25. File Uploading Issues - (2)
    i have never tried to have files uploaded and i am still not able to do so. here is the codes that i
    am using right out of the php manual and it still isnt working. i have also listed the
    warnings/errors listed on the resulting page. my permissions are set to 777 also. i have a folder
    set up on my server as "uploads". i am however not sure if i have a default temp folder on my
    server. can anyone help me figure out what i am not doing correctly or what my next step is? this
    is the form that i am using: html Code: CODE <form enctype="multipart/form-data" ...
  26. Add An Uploading Tool - add an uploading tool (11)
    im really interested in making a page, which will allow visitors to upload there own image to my
    site, without registering or anything. they simply browse their pc and upload an image which they
    like. and that will be hosted by me on my site. ive been looking on the internet and can't
    really find anything, because i don't know what to really call this thing. if anyone could
    help, i would really apreaciate it... thankz...
  27. Help With Reading Files - Read and replace/insert data from form (5)
    Hi, Does anyone know how i can do this, or scripts that will work on Trap17's servers and will
    do the following: I have a .doc file form. Which i want to have filled in automactically, by HTML
    Form and emailed to my address, with the data filled in. Any ideas? I have heard many differing
    things, like XML, RTF, DOC, PDF... I have serched through many places and come up empty handed with
    anything that works. /sad.gif' border='0' style='vertical-align:middle' alt='sad.gif' /> ...
  28. Blog Using Php: Files Or Mysql? - (9)
    I've been planning to put up a blog on my site but I'm having trouble with how I should do
    it. I don't really know how blogs are made. Is it a database or a flat file? I know how to
    write to a file using PHP but I still don't know how to put the last entry on the top of the
    file. That's a big issue in the shoutbox I made for my site. If you look at it, the shouts
    keep appending at the end of the file and I want it to be on top. Can anyone help me please? It
    would be nice if you'd give me detailed instructions on how to put it up, with files...
  29. What Does This Error Mean? - on files and streams (3)
    Can anyone help me with this error? Warning: fopen(guests\guest.txt): failed to open stream:
    Permission denied in /home/leiaah/public_html/images/goguest.php on line 28 Warning: flock():
    supplied argument is not a valid stream resource in /home/leiaah/public_html/images/goguest.php on
    line 30 This is part of the code I wrote // open file for appending $fp =
    fopen("guests\guest.txt", 'a'); flock($fp, LOCK_EX); This works well with my
    offline server but I'm having the error after I published it and when I view it online. Help&...
  30. Uploading Files - Let's do it! :) (0)
    Here, I'll show you how to upload files to the server by your browser. upload.php: CODE
    <? $sizelimit = "280000"; // file size limit $patch =
    "/home/yoursite/public_html/files"; // patch to where the uploaded files will be saved -
    change it if($file != ""){ // if $file is set $file_ext =
    explode(".",$file_name); // check for the extension if($file_size
    > $sizelimit) // check if file size is bigger than the limit die("This file is
    too big."&...



Looking for uploading, files

Searching Video's for uploading, files
Getting List
Of
Directories
And Files
Using Php
PHP Function
for
Directory
and File
List
Need Some
Help In File
Browser
listing all
sub folders
and files in
them.
Download
Script For
Mp3 Files
Change
Permission
With Php
Code code to
change
files'
and
folders'
permissions?
Need Help
Uploading
Php
Grabt Access
To My
Protected
Files grabt
access to my
protected
files
Security
Issue
Writing
Files
Security
issue
writing
files
[php]simple
Flat File
Text
Manipulator
Example on
how to use
forms to
write to
files in PHP
Forms, Text
Files, And
Php For A
Signature
Generator.
Help a
little.
Reading
Files From
Directory To
Array, And
Using
$_get
To Get Them
Simple way
to manage
lot's of
files
Uploading A
Photo...
Writing To
Files And
Such
Files
Required?
How To Sort
Files Of A
Directory
using Php
Displaying
Files Of A
Directory
Uploading
From Url any
scripts?
Logging
Dowload
Files From
Your Server
Onto A Html
File
How To List
Files In A
Directory +
Subdirectory
And Then Use
Them.
Directory
Files
Displaying
Displaying
Latest Added
Files Of
Subdirectori
es ?
What Kind Of
Files Are
.lib
Here's A
Nice And
Interesting
Way To Make
Comments In
your php
files
I Need Help
With
Wordpress/ph
p I am lost
with these
files!
Help With
Removing
Files And
Folders
removing all
files from a
directory
File
Uploading
Issues
Add An
Uploading
Tool add an
uploading
tool
Help With
Reading
Files Read
and
replace/inse
rt data from
form
Blog Using
Php: Files
Or Mysql?
What Does
This Error
Mean? on
files and
streams
Uploading
Files
Let's do
it! :)
advertisement



Help With Uploading Files!



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE