Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
Old 06-14-2008, 11:36 PM PHP and Javascript
Average Talker

Posts: 20
Name: Levi
Trades: 0
Hey, this is more of a question about PHP and Javascript working together. What I am trying to do is build a file upload page where when submit is clicked all the info about the file is sent to a PHP script that uploads it to the correct folder.

Currently it does not upload because the file properties are not carried over to the PHP page because I am using jQuery to post the information to the PHP page. It seems to only be posting the name and nothing else.

So my question boils down to, is there a way to pass file information to PHP using Javascript? I know that I have been reading about how to do it with iFrames. I am not a real big fan of iFrames however and would like to find another work around.


Thanks for any info,
Levi
Levi_ is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-15-2008, 12:10 AM Re: PHP and Javascript
nyef's Avatar
Ultra Talker

Posts: 265
Name: Lucas
Trades: 0
Well, using javascript to initiate http calls (whether GETting or POSTing) involves ajax stuff, xmlhttp object and all that. You can send text via get or post just fine, but I don't know that I've ever seen any way to send a binary file through xmlhttp. Not saying it can't be done but if it is possible I have no idea how.

Is it that critical to upload the file through a clientside http call? There are usually only one or a few places in a file application where you need to allow file uploads. Would it be okay to do it the normal way through a regular form submission?
__________________
~nyef

Please login or register to view this content. Registration is FREE
nyef is offline
Reply With Quote
View Public Profile Visit nyef's homepage!
 
Old 06-16-2008, 12:15 PM Re: PHP and Javascript
willcode4beer's Avatar
Super Moderator

Posts: 1,533
Name: Paul Davis
Location: San Francisco
Trades: 1
What properties are you trying to get?

Remember, even with that code, sending a file is still just an HTTP POST.
__________________

Please login or register to view this content. Registration is FREE

willcode4beer is offline
Reply With Quote
View Public Profile
 
Old 06-16-2008, 05:25 PM Re: PHP and Javascript
Average Talker

Posts: 20
Name: Levi
Trades: 0
I am trying to get the file in general I am working on an Ajax page. I can send the files name fine using a Post but when I try and retrieve the data about the file such as file size, type of file etc.. nothing is coming through with the name. If you will notice I use jQuery to post to the php file on submit so that is where I think the problem may lie.

This is the form code I am using:
Code:
<form method="post" enctype="multipart/form-data" name="brancato" id="brancato">
<input type="file" name="uploaded_file" id="uploaded_file">
<input type="submit" name="sendForm" id="sendForm" value="Upload">
</form>
this is the jQuery I coded:
Code:
$(document).ready(function() {
    $("div#yes").hide().fadeOut(100);
    $("div#no").hide().fadeOut(100);
    
    $("form#brancato").submit(function(){
         $.post("upload.php",{ uploaded_file: $("#uploaded_file").val()},
        function(xml) {
            if($("status",xml).text() == "0")
            {
                $("div#yes").hide();
                $("div#no").fadeIn(300);
           }
          
           else if($("status",xml).text() == "1")
            {
                $("div#no").hide();
                $("div#yes").fadeIn(300);
           }
       });
       return false;
   });
});
and this is the php page I coded to work with the file info:
Code:
///
// UPLOAD FILE //
///
$folder = "uploads/";
$folder = $folder . basename( $_FILES['uploaded_file']['name']);
$upload_size = $_FILES['uploaded_file']['size'];
$error = $_FILES['uploaded_file']['error'];

// CHECK IF INPUT WAS A VALID FILE //
$input = $_POST['uploaded_file'];
    list($garbage,$extension) = split('[.]',$input,2);
$yes = 0;

switch ($extension) 
{
case "zip":
    $yes = 1;
    break;
case "rar":
    $yes = 1;
    break;
case "jpg":
    $yes = 1;
    break;
default:
        $yes = 2;
        break;
}
//
///
// UPLOAD //
///
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $folder))
{
$yes = 1;    
}
else
{
$yes = 0;
}

if($yes == 1)
{
header( "Location: index.php?upload=yup" ) ;
}
if($yes == 0)
{
header( "Location: index.php?upload=nope" ) ;
}
if($yes == 2)
{
header( "Location: index.php?upload=invalid_file" ) ;    
}
?>
I don't really need to use Ajax to do this, I just want to for learning purposes and to see if it can be done.
Levi_ is offline
Reply With Quote
View Public Profile
 
Old 06-17-2008, 02:50 PM Re: PHP and Javascript
willcode4beer's Avatar
Super Moderator

Posts: 1,533
Name: Paul Davis
Location: San Francisco
Trades: 1
The thing is jQuery doesn't have access to that information in order to send it to the server. It's a security violation for the browser.

Your options are going to be using a signed applet/activex control on the client or write some server code to read the file. Copying to temp space should be fine for most purposes. Just be sure to configure the server to limit the maximum allowable file size (to prevent DOS attacks).

Best way to get file type is to ignore the extension and read the magic number.
__________________

Please login or register to view this content. Registration is FREE

willcode4beer is offline
Reply With Quote
View Public Profile
 
Old 06-17-2008, 05:56 PM Re: PHP and Javascript
Average Talker

Posts: 20
Name: Levi
Trades: 0
I don't really have the knowledge for that currently so I think I may just not let jQuery handle it. Maybe i'll take a look at using an iFrame on that page as most tutorials show being used.

Thanks for all the help,
Levi
Levi_ is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to PHP and Javascript
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.23950 seconds with 12 queries