hi fellas,

Im using javascript framework in uploading image via ajax but the problem is $_FILE is empty here s my code:

<form action="upload.php" method="post" enctype="multipart/form-data" id="upload" name="upload">
                <input type="file" name="file" id="file" style="border:none"/></center>
</form>

js

var url = 'upload.php';
        new Ajax.Request(url, {
            method: 'post',
            contentType: 'multipart/form-data',
            encoding: 'UTF-8',
            parameters: $('upload').serialize(true),
        onSuccess: function(transport) {
                                       var notice = $('error');
                                       if (transport.responseText=='success'){
                                            alert('success');
                                        }else{
                                           alert(transport.responseText);
                                        }
                                    }
        });

php
echo $_FILES["file"]["name"] // is empty....
uploading script here....

Member Avatar for LastMitch

@code739

I don't see anything wrong with the FORM plus you didn't really provde much PHP code!

This question is more related to Javascript. I think it's best to put this in Javascript section rather than in PHP section.

yah, sorry...
heres my php code:

<?php
include "db_handle.php";
session_start();
$allowedExts = array("jpg", "jpeg", "gif", "png","bmp","pjpeg");
$filename = explode(".",$_FILES["file"]["name"]);
$extension = end($filename);
$pid = $_POST['pid'];

if (in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    $error =  "Return Code: " . $_FILES["file"]["error"] . "<br />";
    echo $error;
    }
  else
    {
      move_uploaded_file($_FILES["file"]["tmp_name"],"uploads/" . $_FILES["file"]["name"]);
      $stmt = "select photo from media_person where personid=".$pid;
      $parse = mysql_query($stmt);
      $oldPic = mysql_fetch_array($parse);
        if(file_exists($oldPic['photo'])){
            unlink($oldPic['photo']);
        }
      $sql = "update media_person set photo='"."uploads/".$_FILES["file"]["name"]."' where personid=".$pid;
      $query = mysql_query($sql) or die(mysql_error());
      echo 'success';
    }
  }
else
  {
  //the process always enter here cause $_FILE is empty...
  $error = 'not a valid file';
  echo $error;
  }

?> 

thanks any way

Member Avatar for LastMitch

@code739

Have you try var_dump($_FILES); to see whether file is actually getting posted correctly?

Then you can try this to see whether there is a file:

if(!empty($_FILES["file"]["name"] > 0)) {}

if i use var_dump($_FILES) int prints array buy empty....

Member Avatar for LastMitch

@code739

Have you try this:

if(!empty($_FILES["file"]["name"] > 0)) {}

-

if (in_array($extension, $allowedExts))
  {
  if(!empty($_FILES["file"]["error"] > 0))
    {
    $error =  "Return Code: " . $_FILES["file"]["error"] . "<br />";
    echo $error;
    }

What did you get?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.