Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,676 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,199 people online right now. Registration is fast and FREE... Join Now!




Help with right code for saving and retrieving in php

 
Reply to this topicStart new topic

Help with right code for saving and retrieving in php, In need of code for browsing a file, saving in database and retrieving

BOGHOL2
post 9 Aug, 2007 - 10:28 PM
Post #1


New D.I.C Head

*
Joined: 9 Aug, 2007
Posts: 3


My Contributions


I need of a php code for browsing, saving and retrieving a file/document/picture in php. This code has errors that I have failed to overcome.

CODE

<html>
<head>
<title>A simple file upload form</title>
</head>
<body>
<form action="do_upload.php" enctype="multipart/form-data" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="51200">
<p><strong>File to Upload:</strong> <input type="file" name="fileupload"></p>
<p><input type="submit" value="upload!"></p>
</form>
</body>
</html>


<?php
$file_dir = "\My Documents\.";
foreach($_FILES as $file_name => $file_array) {
    echo "path: ".$file_array['tmp_name']."<br>\n";
    echo "name: ".$file_array['name']."<br>\n";
    echo "type: ".$file_array['type']."<br>\n";
    echo "size: ".$file_array['size']."<br>\n";
    if (is_uploaded_file($file_array['tmp_name'])) {
        move_uploaded_file($file_array['tmp_name'],
           "$file_dir/$file_array[name]") or die ("Couldn't copy");
        echo "file was moved!<br><br>";
     }
}
?>

User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 10 Aug, 2007 - 01:57 AM
Post #2


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,933



Thanked 118 times

Dream Kudos: 8525

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


First -> What errors are you getting, we cannot help you debug if we don't know whats going on

Second -> If you're simply uploading a file why are you using foreach? foreach is for working with like Arrays, and for uploading a file you don't need an Array do you?

When uploading a single file all you really need is

CODE

/ Where the file is going to be placed
$target_path = "your_directory/";

/* Add the filename to the path.  
Result is "your_directory/filename.extension" */
$target_path = $target_path . basename( $_FILES['your_upload_control']['name']);
$_FILES['your_upload_control']['tmp_name'];

$target_path = $target_path . basename( $_FILES['your_upload_control']['name']);

if(move_uploaded_file($_FILES['your_upload_control']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['your_upload_control']['name']).
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}


No need to work with an Array for a file.

Third -> You have your $target_path as \My Documents\. Are you attempting to let people upload files to your My Documents folder? This isn't going to work, the directory needs to be a directory on your web server, not your personal computer (unless they are both in the same, even then don't use My Documents).

I hope this helps you out, and gets you going in the right direction smile.gif

Happy Coding!
User is offlineProfile CardPM

Go to the top of the page

snoj
post 10 Aug, 2007 - 06:10 AM
Post #3


$Null

Group Icon
Joined: 31 Mar, 2003
Posts: 3,304



Thanked 5 times

Dream Kudos: 700
My Contributions


No need to post TWO topics.

I've close the other.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 06:27AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month