tags:

views:

335

answers:

1

I want to upload mp3 files to server by using ftp in php language.I did a raw code script for this.In server image,pdf,doc files are uploaded but i can not upload mp3 files.

Plz,anyone would help me??I am in serious trouble!!!

It shows file does not exist when i am going to upload mp3 files

Here is my code,--

<?php

// FTP Configuration
$FTP_User = $_POST["username"];
$FTP_Pass = $_POST["password"];
$FTP_Host = "###";
$FTP_Root = "$dir";

  $action=$_GET["action"]; 
  $username=$_POST["username"];
  $password=$_POST["password"];
  $dir=$_POST["dir"]; 
  $Filename=$_POST["Filename"];
  $MAX_FILE_SIZE= $_POST["MAX_FILE_SIZE"]; 

// If the form was submitted
if (isset($action) && $action == "submit") {

if ($_FILES['File_1']['size'] > $MAX_FILE_SIZE)
{
echo "Error: $Filename exceeds upload limit";
exit;
}

echo $_FILES['File_1']['size'];
echo $MAX_FILE_SIZE;

if ($dir == "-1")
{
echo "Error: Please choose a File Type to upload";
exit;
}

// Connect to the ftp address
$Connect = ftp_connect($FTP_Host);

if (!$Connect)
{
echo "Error: Could not connect to ftp server<br>";
exit;
}

echo "Connected to $FTP_Host<br>";

// Login
$login = ftp_login($Connect, $FTP_User, $FTP_Pass);

//Turns passive mode on
$passive = ftp_pasv ($Connect, true );

echo "Current directory is now: " . ftp_pwd($Connect) . "<br>";

if (ftp_chdir($Connect, "$FTP_Root"))
{
echo "Current directory is now: " . ftp_pwd($Connect) . "<br>";
} else echo "Cannot change directory";

$contents = ftp_nlist($Connect, ".");

// output $contents
print_r($contents);

// check upload status
if (!passive){
echo "Failed to enter passive mode.<br>";
}
else {
echo "Entered passive mode.<br>";
}

if (!$login)
{
echo "Error: Could not log on as $FTP_User<br>";
ftp_quit($Connect);
exit;
}

echo "Logged in as $FTP_User<br>";

// Set the filename to be uploaded
$Filename = $_FILES['File_1']['name'];
$myFile = $_FILES['File_1'];

//echo 'Filename is :'. $Filename;
print_r($myFile);
if (file_exists($Filename))
{
echo "The file $Filename exists<BR>";
}
else
{
echo "The file $Filename does not exist<BR>";
}

$destination_file = $FTP_ROOT.$_FILES['File_1']['name'];

// Set the local resource (the file that will be uploaded)
$file = $myFile['tmp_name'];

// If the file was successfully uploaded
$upload = ftp_put($Connect, $destination_file, $file, FTP_BINARY);

if (!$upload)
{
// Show success message
echo "There was a problem uploading $destination_file";
}
else
{
// Else show error message
echo "Successfully uploaded $Filename";

}
ftp_close($Connect);
}

?>

Form Code:

<html>
<head>
<title> PHP FTP Upload Test </title>
</head>
<body>
<table>
<form method="post" action="file_upload.php?action=submit" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000">
<tr>
<td>Username:</td>
<td><input type="text" name="username" size="30"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" size="30"></td>
</tr>
<tr>
<td>File Type:</td>
<td><select name="dir">
<option value="-1">File Type</option>
<!--<option value="/httpdocs/charbel/upload/">Image</option>
<option value="/httpdocs/charbel/upload/">PDF</option>
<option value="/httpdocs/charbel/upload/">MP3</option>
<option value="/httpdocs/charbel/upload/">Video</option>
-->
<option value="/public_html/">Image</option>
<option value="/public_html/">PDF</option>
<option value="/public_html/">MP3</option>
<option value="/public_html/">Video</option>

</select>
</td>
</tr>
<tr>
<td>File:</td>
<td><input type="file" name="File_1" size="70"></td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" value="Submit"></td>
</tr>
</form>
</table>
</body>
</html>
+2  A: 

If you have the cURL module installed or can install it, I recommend using it. Here's a tutorial that does what you are trying to do: http://www.web-development-blog.com/archives/tutorial-ftp-upload-via-curl/

If cURL is out of the question, you could check if the file exists with file_exists($myFile['tmp_name']), because I'm fairly sure the function is expecting an actual filename.

Kaivosukeltaja
Agreed. file_exists needs to be run on the temp name, not the name that was sent with the file.
Narcissus