Putting files in a directory

This is a discussion on "Putting files in a directory" within the PHP Forum section. This forum, and the thread "Putting files in a directory are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jun 26th, 2006, 16:58
Junior Member
Join Date: Jun 2006
Location: usa
Age: 29
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation Putting files in a directory

Ok, It want to upload a file and put it in a directory..
so i can call it later, it just seemt o be happing problem puttin the file in a directory..

can anyone please help here is the code:
PHP: Select all

 
<?php
$picture1
=$_FILES['picture1']['tmp_name'];
var_dump($_FILES);
 
$upfile $_SERVER['/home/httpdocs/images/'].$picture1_name;
print_r($picture1);
if(
move_uploaded_file($picture1$upfile))
{
echo 
"problem: could not move file into directory";
exit;
}
 
echo 
"File uploaded successfully<br><br>";
$fp fopen($upfile"r");
$fp fopen($upfile"w");
fwrite($fp);
fclose($fp);
echo 
"Preview of uploaded file contents:<br><hr>";
echo 
"<br><hr>";
?>
and this is what i am displaying

PHP: Select all

 
array(1) { ["picture1"]=> array(4) { ["name"]=> string(17"1143660146y1u.jpg" ["type"]=> string(11"image/pjpeg" ["tmp_name"]=> string(4"none" ["size"]=> int(0) } } noneFile uploaded successfully
 
 
Warning
fopen("1143660146y1u.jpg""r") - No such file or directory in /home/httpd/vhosts/mass-ad.com/httpdocs/insert_data.php on line 124
 
Warning
fopen("1143660146y1u.jpg""w") - Permission denied in /home/httpd/vhosts/mass-ad.com/httpdocs/insert_data.php on line 125
 
Warning
Wrong parameter count for fwrite() in /home/httpd/vhosts/mass-ad.com/httpdocs/insert_data.php on line 126
 
Warning
Supplied argument is not a valid File-Handle resource in /home/httpd/vhosts/mass-ad.com/httpdocs/insert_data.php on line 127
Preview of uploaded file contents

Reply With Quote

  #2 (permalink)  
Old Jun 27th, 2006, 09:53
Junior Member
Join Date: Jun 2006
Location: Sheffield, South Yorkshire, UK
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Putting files in a directory

PHP: Select all


<?php

        
// FILE:  generic/uploadfile.php
        // Desc: Generic Include for file Uploads
        // Date: 12 Dec 2005
        // written by David Wales of s10sys.com

        // this is the holder of your uploaded file.
        
global $_FILES;

        
// set this if your file is large so you don't time out on upload
        
set_time_limit(240);
        
        
// this is a linux example of the path variable.
        
$PATH '/your/destination/directory/';

        
// this would be windows...?
        // $PATH = 'c:\your\destination\directory\';

        // upload the file to a data directory.
        
if (is_uploaded_file($_FILES['file']['tmp_name']))
        {
            if(!
move_uploaded_file($_FILES['file']['tmp_name'], $PATH.$_FILES['file']['name']))
            {
                die( 
'file upload is complete...'."<br>".'Failed to move uploaded file... ScriptStopped!' );
            }
        }
        else
        {
            die( 
"<br>".'Security - possible file upload attack... ScriptStopped!' );
        }

        
// now drop out back to original include point.
?>
I always use the same bit of code to upload my files, this file above is my upload script and it works every time. Anything else you want to do with your upload should be part of a seperate script.

For example, save the above file as uploadfile.php and create a new file called myfooupload.html and make it look like the example below, point your form action at the myfooupload.html file and see what happens.

PHP: Select all


<?php

   
// test uploading of a file.
   
   
if(!isset($_FILE))
   {
       
// show your upload form.
       
include_once('./myuploadform.html');
   }
   else
   {
       
// include the generic upload script.
       
include_once('./uploadfile.php');

       
// include a seperate file that now does stuff to the uploaded file.
       
include_once('./modifyfooupload.php');
   }

?>
By doing things in this manner you can check that the uploadfile.php has been successfull and be happy that it will work for all uploads, then all you need worry about is what your modifyfooupload.php file does to the uploaded file.

On a side note it is worth changing your fopen statments don't make any sense at all and don't actually do anything other than maybe mess the files up? Remove these lines from your code.

PHP: Select all


$fp 
fopen($upfile"r");
$fp fopen($upfile"w");
fwrite($fp);
fclose($fp); 
Also when using an fopen you should always check the return as below:-

PHP: Select all

<?php

$fp 
fopen($filename,'r');
if(!
is_resource($fp))
{
    die(
'could not open/create '.$filename.' for writting/reading');
}

$filecontents '';

while(
$buff fread($fp,1024))
{
   
$filecontents.= $buff;
}

fclose($fp);

echo 
'<pre>'.$filecontents.'</pre>';

?>
Hope this helps
Reply With Quote
Reply

Tags
putting, files, directory

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
putting a widget in CSS kobesunset Starting Out 5 Jun 8th, 2008 19:22
Putting C# into an ASP.NET Page. alexgeek ASP.NET Forum 2 Feb 5th, 2008 17:16
Directory of files not in alphabetical order dkintheuk PHP Forum 5 Dec 16th, 2005 09:34
putting a movie on the web rmlittleone Web Page Design 5 Aug 4th, 2005 16:24
Putting PHP in your web page Jodi PHP Forum 13 Jan 10th, 2005 20:50


All times are GMT. The time now is 21:07.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43