homepage Welcome to WebmasterWorld Guest from 184.73.52.98
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
php upload mp3
Pico_Train




msg:4357998
 3:34 pm on Sep 2, 2011 (gmt 0)

Hi!

I'm uploading mp3 files to my server with php and in the form there is enctype="multipart/form-data"

I can see the file on the server in my ftp program but if I try and CHMOD or delete it, it tells me the file does not exist.

If I try and enter the path to the file in my browser address bar, I also get a file does not exist.

I have no clue what's wrong. Any ideas? Thanks in advance!

 

penders




msg:4358004
 3:52 pm on Sep 2, 2011 (gmt 0)

In your FTP program does it show as the correct size? Disconnect/reconnect FTP - still there? Has it finished uploading?

Does your hosting account allow mp3 files to be uploaded?

Pico_Train




msg:4358013
 4:09 pm on Sep 2, 2011 (gmt 0)

Thanks for the reply. The hosts say yes it's fine. I did the FTP check and they are there.

I'm using an mp3 player and it works fine locally, the live setup just isn't going.

Any other ideas?

penders




msg:4358031
 4:59 pm on Sep 2, 2011 (gmt 0)

Presumably move_uploaded_file() was successful?

How big is the file? (Wondering if any file upload limits on PHP, but you should get a fail state at some point if this was the case?)

If you upload the file via FTP to the same location is it OK?

Pico_Train




msg:4358065
 6:31 pm on Sep 2, 2011 (gmt 0)

Yes, move_uploaded_file is succesful. I move 2 files to different places and locally they work. There are no error messages, as far as I can tell. It works fine locally.

The file is 1mb. My form is set to accept up 12000000 in the max_file_size. Hosts/PHP side size is not an issue.

I will try that and see how it goes tomorrow. Could be a good idea.

Pico_Train




msg:4358093
 8:15 pm on Sep 2, 2011 (gmt 0)

If I load the file through FTP, it works...so...what now?

penders




msg:4358119
 9:11 pm on Sep 2, 2011 (gmt 0)

I can see the file on the server in my ftp program but if I try and CHMOD or delete it, it tells me the file does not exist.


Is this when you try to chmod/delete via FTP? The FTP user is likely to be a different user than the one you connect with through PHP. You are likely to have to change the perms via PHP in order to make it accessible to the FTP user (and the web user?!). I'm not sure, but this could be a permissions problem? Have you uploaded other files using your script to this server?

Pico_Train




msg:4358223
 5:49 am on Sep 3, 2011 (gmt 0)

Yes via FTP.

OK so i guess I need to look into php permissions.

I've uploaded other files but just jpg and that was fine.

Thanks for the insight. I will look into permissions.

Pico_Train




msg:4358225
 6:05 am on Sep 3, 2011 (gmt 0)

OK tried that and it doesn't like it either, the file doesn't exist where it is looking, again.

I think something is going wrong in the script because the file I uploaded with FTP works fine. The files uploaded with the script and the files uploaded via FTP have the same permissions - 644 1 works, the other doesn't.

[edited by: Pico_Train at 6:12 am (utc) on Sep 3, 2011]

Pico_Train




msg:4358226
 6:06 am on Sep 3, 2011 (gmt 0)

Here is the upload script code


if(!empty($_FILES))
{
{
$title = $db->escape($_POST['title']);
//testing
//$fullpath = "../files/music/".$profile_id."/";
//live
$fullpath = "../../files/music/".$profile_id."/";
//echo $fullpath;exit();
$previewpath = $_SERVER['DOCUMENT_ROOT']."/files/".$profile_id."/";

if(!file_exists($fullpath))
{
//testing
//mkdir("../files/music/".$profile_id."/");
//live
mkdir("../../files/music/".$profile_id."/");
}else{

}

if(!file_exists($previewpath))
{
mkdir($_SERVER['DOCUMENT_ROOT']."/files/".$profile_id."/");
}else{

}

$extlimit = "yes"; //Limit allowed extensions? (no for all extensions allowed)

//List of allowed extensions if extlimit = yes

$limitedext = array(".mp3");

//preview details
$preview_type = $_FILES['preview']['type'];

$preview_name = $_FILES['preview']['name'];

$preview_size = $_FILES['preview']['size'];

$preview_tmp = $_FILES['preview']['tmp_name'];

//fullsong file details
$fullsong_type = $_FILES['full_song']['type'];

$fullsong_name = $_FILES['full_song']['name'];

$fullsong_size = $_FILES['full_song']['size'];

$fullsong_tmp = $_FILES['full_song']['tmp_name'];

//check if you have selected a file.

/*if(!is_uploaded_file($preview_tmp) || !is_uploaded_file($fullsong_tmp)){

//echo "Error: Please select a file to upload!. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";

exit(); //exit the script and don't process the rest of it!

}*/

//check the file's extension

$ext_preview = strrchr($preview_name,'.');

$ext_preview = strtolower($ext_preview);

$ext_full = strrchr($fullsong_name,'.');

$ext_full = strtolower($fullsong_name);

//uh-oh! the file extension is not allowed!

/*if (($extlimit == "yes") && (!in_array($ext_preview,$limitedext)) && (!in_array($ext_full,$limitedext))) {

$message = "These types of files cannot be uploaded. Please upload an MP3 file. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";

//echo $message; exit();

}*/

//so, whats the file's extension?

$getPreviewExt = explode ('.', $preview_name);

$file_preview_ext = strtolower($getPreviewExt[count($getPreviewExt)-1]);

$getFullExt = explode ('.', $fullsong_name);

$file_full_ext = strtolower($getFullExt[count($getFullExt)-1]);

//create a random file name

//$next_id = $model->getnextMusicId();


$rand_preview_name = $title."_preview.".$file_preview_ext;
$rand_full_name= $title.".".$file_full_ext;

$file_preview_exists = "$previewpath$rand_preview_name";
$file_full_exists = "$fullpath$rand_full_name";

//chmod("../../files/music/".$profile_id."/".$rand_full_name, 755);
//chmod($_SERVER['DOCUMENT_ROOT']."/files/".$profile_id."/".$rand_preview_name, 755);

move_uploaded_file($_FILES['preview']['tmp_name'], "$previewpath$rand_preview_name ");
move_uploaded_file($_FILES['full_song']['tmp_name'], "$fullpath$rand_full_name");



if (file_exists($file_full_exists) && file_exists($file_preview_exists))
{
/*$message = "<b>The file ". basename( $_FILES['preview']['name']).
" and ". basename( $_FILES['full_song']['name']).
" have been uploaded to your profile.</b><br><br>";*/
} else{
/*$message = "<b>The files ". basename( $_FILES['preview']['name']).
" and ". basename( $_FILES['full_song']['name']).
" were not uploaded. Please try again.</b><br><br>";*/
}
$uploaded = $ins->insertMusic($_POST, $fullsong_type, $preview_type,$rand_full_name, $rand_preview_name);}}}


penders




msg:4358251
 10:47 am on Sep 3, 2011 (gmt 0)

Before looking at you script...

The files uploaded with the script and the files uploaded via FTP have the same permissions - 644 1 works, the other doesn't.


But the owner is likely to be different. It is a bit strange that the file apparently does not exist (even from PHP)?! Will try and have a look at your script a bit later...

Pico_Train




msg:4358253
 11:32 am on Sep 3, 2011 (gmt 0)

Thanks for your help Penders.

Pico_Train




msg:4358255
 11:44 am on Sep 3, 2011 (gmt 0)

If I go into the filemanager in my cpanel, I can see all the files.

The one uploaded through FTP has the type listed as audio/mpeg- this is correct.

The ones uploaded through my script have the type listed as text/x-generic - this is incorrect.

So...I reckon that's the problem

Pico_Train




msg:4358743
 9:52 am on Sep 5, 2011 (gmt 0)

Well well...

Fixed!

Errant space in my move_uploaded_files code - Nooooooooooo!

move_uploaded_file($_FILES['preview']['tmp_name'], "$previewpath$rand_preview_name "); - See space before "

should have been

move_uploaded_file($_FILES['preview']['tmp_name'], "$previewpath$rand_preview_name");

Thanks for talking through it, always helps!

penders




msg:4358748
 10:58 am on Sep 5, 2011 (gmt 0)

Glad you got it sorted! Spaces in filenames can be troublesome, but a space at the end - ow! Should be illegal. :) Windows seems to silently discard spaces at the end of the filename (after the extension), so that might explain why it worked OK locally (assuming your test server is Windows).

May be a lesson not to use variable parsing in this instance. String concatenation would be less error prone (and more efficient).

Matthew1980




msg:4359167
 6:25 pm on Sep 6, 2011 (gmt 0)

Hi all,

I was wondering how this thread would end up.. I always try to advocate to people about this sort of thing:-

move_uploaded_file($_FILES['preview']['tmp_name'], $previewpath.$rand_preview_name);

Because this is a variable name, it doesn't need to be quoted - and this is one of the reasons why - unquoted would probably have avoided this erroneous whitespace. Realistically, you need to concatenate the variables together so that php can see what's actually going on - this is good practise really.

At least you have managed to fix the issue.

Also, I notice that you're passing $_POST into a class function; this isn't really needed because by definition these globals are available throughout the scope of your script so long as they are set/have state.

Have fun with the rest of the project.

Cheers,
MRb

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved