Solved

Uploading mp3 file failure

Posted on 2006-07-24
9
458 Views
Last Modified: 2008-02-01
Hello Experts,
I have a script I've been using to upload pdf's to our website.  I've been asked to add an accompanying mp3 file. So I created another file control and
set it to upload just like the pdf. The problem is, it doesn't upload. When I go to echo the tmp_name I don't get anything. Here is the script:
<?php
      echo $_FILES['binFile']['tmp_name'];
      echo $_FILES['binFile']['name'];
      echo $_FILES['radioFile']['tmp_name'] . " tmp ";
      echo $_FILES['radioFile']['name'];

function Database(){
                  include($_SERVER['DOCUMENT_ROOT'].'/Connections/NEOD.php');
                  $file = $_FILES['binFile']['name'];
                  $radio_file =  $_FILES['radioFile']['name'];
                  if ($_POST['btnArticleType'] == "BI"){
                        $link = "/news/black_ink/".$file;
                  }else if ($_POST['btnArticleType'] == "CQ"){
                        $link = "/news/corner/".$file;
                  }else if ($_POST['btnArticleType'] == "NS"){
                        $link = "/news/press/".$file;
                  }else if ($_POST['btnArticleType'] == "PS"){
                        $link = "/news/published/".$file;
                  }else if ($_POST['btnArticleType'] == "RD"){
                        $link = "/news/radio/".$file;
                  }
                  $strTitle = addslashes(nl2br($_POST['txtTitle']));
                  $dteDate = ($_POST['txtDate']);
                  $strSynopsis = addslashes(nl2br($_POST['txtSynopsis']));
                  $strTopicArea = $_POST['cboTopicArea'];
                  $strAuthors = addslashes(nl2br($_POST['txtAuthor']));
                  $strTopic = addslashes(nl2br($_POST['txtTopic']));
                  $strCategory = $_POST['cboCategory'];
                  
                  
                  $sql = "INSERT INTO fl_article2 ";          
                $sql .= "(Title, Link, Date, Filename, Synopsis, Topic_Area, Topic, Authors, Category, Type, mp3_file) ";
                $sql .= "VALUES ('$strTitle', '$link', '$dteDate', '$file', '$strSynopsis', '$strTopicArea', '$strTopic', '$strAuthors', '$strCategory','$_POST[btnArticleType]','$radio_file')";
                echo($sql);
                        //Set $result to contain the result set of the SQL statement
                  $result = mysql_query($sql,$neod);
                  if (!$result){
                        echo "<h2>Upload to database failed</h2>";
                        echo $sql;
                  }else{
                        echo "<h2>Upload to database successful!</h2>";
                        echo  "<p><a href=http://www.website.com/press/SelectFile.php target='_blank'>View Release Page</a></p>";
                  }
}

function FTP($radio_file) {
                  // set up basic connection
            $conn_id = ftp_connect("www.ftp_site.com");

                  // login with username and password
            $login_result = ftp_login($conn_id, "ftp_user", "ftp_pass");

                  // check connection
            if ((!$conn_id) || (!$login_result)) {
              echo "FTP connection has failed!<br>";
                  echo "Attempted to connect to www.website.com for user cab<br>";
                 die;
          } else {
              echo "<p>Connected to www.ftp_site.com, for user cab</p>";
          

      // upload the file
            if ($_POST['btnArticleType'] == "BI"){
                  $destination_file = "/html/sd/news/black_ink/".$_FILES['binFile']['name'];
            }else if ($_POST['btnArticleType'] == "CQ"){
                  $destination_file = "/html/sd/news/corner/".$_FILES['binFile']['name'];
            }else if ($_POST['btnArticleType'] == "NS"){
                  $destination_file = "/html/sd/news/press/".$_FILES['binFile']['name'];
            }else if ($_POST['btnArticleType'] == "PS"){
                  $destination_file = "/html/sd/news/published/".$_FILES['binFile']['name'];
            }else if ($_POST['btnArticleType'] == "RD"){
                  $destination_file = "/html/sd/news/radio/".$_FILES['binFile']['name'];
            }
            $source_file = $_FILES['binFile']['tmp_name'];
            $upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
            
            
            $source_file = $radio_file;
            $upload = ftp_put($conn_id, "/html/sd/news/radio/".$_FILES['radioFile']['name'], $source_file, FTP_BINARY);
            echo $source_file." source";
            


 }
 


      // check upload status
      
            if (!$upload) {
              echo "<p>FTP upload has failed!</p>";
                     echo "<p>File size may be over 200K</p>";
             } else {
              echo "<p>Uploaded $source_file to www.ftp_site.com as $destination_file</p>";
                   echo "<p>Loading file to database</p>";
                  Database();
             }

      // close the FTP stream
            ftp_close($conn_id);
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/portal.dwt.php" codeOutsideHTMLIsLocked="false" -->
    <head>
    <!-- InstanceBeginEditable name="doctitle" -->
<title></title>
<!-- InstanceEndEditable -->
    <link href="../../ASPNETPortal.css" type="text/css" rel="stylesheet">
            <!-- InstanceBeginEditable name="head" -->





<!-- InstanceEndEditable -->
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /></head>
    <body leftmargin="0" bottommargin="0" rightmargin="0" topmargin="0" marginheight="0" marginwidth="0">
   
       

<table width="100%" cellspacing="0" cellpadding="0" border="0">
      <tr valign="top">
      <td colspan="2">              

<table width="100%" cellspacing="0" class="HeadBg" border="0">
      <tr valign="top">
      <td colspan="3" class="SiteLink" bgcolor="darkgray" align="right">
                  <span id="Banner_WelcomeMessage" style="color:#EEEEEE;">Welcome <?php echo $_SESSION[UserName]; ?>! <span class=Accent>|</span></span>
                        </td>
      </tr>
      <tr>
      <td width="10" rowspan="2">
      </td>
      <td height="40">
            <span id="Banner_siteName" class="SiteTitle">CAB Intranet</span>
      </td>
      <td align="center">
      </td>
      </tr>
      <tr>
      <td>
<?php include ($_SERVER['DOCUMENT_ROOT'] .'/includes/portal_head.inc'); ?>
      </td>
      </tr>
</table>
      </td>
    </tr>
    <tr>
    <td>
                       
<table width="100%" cellspacing="0" cellpadding="4" border="0">
    <tr height="*" valign="top">
    <td width="5">&nbsp;
    </td>
    <td id="LeftPane" width="170"><div class="level2"><!-- InstanceBeginEditable name="level2" -->
            <?php include ($_SERVER['DOCUMENT_ROOT'] .'/includes/level2_sd.inc'); ?>
          <!-- InstanceEndEditable --></div></td>
      <td width="1">
    </td>
                              <td id="ContentPane" width="*"><table id="_ctl2_t1" cellspacing="0" cellpadding="0">
      <tr valign="top">
            
      </tr>
</table>
<!-- InstanceBeginEditable name="main-content" -->
      <?php
      FTP($_FILES['radioFile']['tmp_name']);
      ?>

<!-- InstanceEndEditable --></td>

                                <td id="RightPane" width="230">
         
<!-- InstanceBeginEditable name="right-column" -->l<!-- InstanceEndEditable --><br>

      
    <br></td>

                                <td width="10">&nbsp;
                                   
                              </td>
        </tr>
      </table>
      </td>
  </tr>
    </table>
       
    </body>
<!-- InstanceEnd --></html>
0
Comment
Question by:ChadMarsh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 5

Expert Comment

by:floorman67
ID: 17167141
is it generating any errors at all either on the web page output, the php logs, or the web server logs ?

0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 17167168
Can you do a comparison between a working PDF script and a non working MP3 script and show us what you changed.

0
 
LVL 2

Author Comment

by:ChadMarsh
ID: 17167291

This is the original code:
$source_file = $_FILES['binFile']['tmp_name'];
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
            
I added this at the bottom to submit the mp3 file.            
$source_file = $radio_file;
$upload = ftp_put($conn_id, "/html/sd/news/radio/".$_FILES['radioFile']['name'], $source_file, FTP_BINARY);
echo $source_file." source";

Still working on the logs. I don't get any errors generated when I submit
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Expert Comment

by:Richard Quadling
ID: 17167350
Why did you change $source_file?

In the original code, this points to the uploaded temporary file. Not an ideal situation as the files uploaded for PHP to handle via $_FILES are not really for userland.

You should move them from there to a safe place of your own.

if (is_uploaded_file($_FILES['bin_file']['tmp_name']) && move_uploaded_file($_FILES['bin_file']['tmp_name'], './uploaded/' . $_FILES['bin_file']['name']))
 {
 // File has been safely moved out of the temporary upload area and into an area for you to work on.
 // ...
 // Once you've finished with it, you should delete it if you don't want it.
 unlink('./uploaded/' . $_FILES['bin_file']['name']);
 }
else
 {
 // Normally this point is reached if the files are wrong or someone is attempting to fool PHP into working with files NOT uploaded to PHP.
 }

0
 
LVL 2

Author Comment

by:ChadMarsh
ID: 17167407
My initial thought was that would run this

$source_file = $_FILES['binFile']['tmp_name'];
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);

Uplad the pdf then run this

$source_file = $radio_file;
$upload = ftp_put($conn_id, "/html/sd/news/radio/".$_FILES['radioFile']['name'], $source_file, FTP_BINARY);

to upload the mp3 all in the same connection one right after the other.
Can php only handle one file in temp at a time?
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 17167497
You can upload many files simultaneously. Just give them proper HTML names.

Can you show the HTML form.
0
 
LVL 2

Author Comment

by:ChadMarsh
ID: 17167537
That's what I thought, but wasn't sure. Here is the HTML

<?php //include('../../../permissions.php');
            include($_SERVER['DOCUMENT_ROOT'].'/Connections/NEOD.php');
            $catsql = "SELECT * FROM fl_article_category";
            $catresult = mysql_query($catsql, $neod);
            $topsql = "SELECT * FROM fl_article_topic_area";
            $topresult = mysql_query($topsql,$neod);
            
            
            ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/portal.dwt.php" codeOutsideHTMLIsLocked="false" -->
    <head>
    <!-- InstanceBeginEditable name="doctitle" -->
<title></title>

<!-- InstanceEndEditable -->
    <link href="../../ASPNETPortal.css" type="text/css" rel="stylesheet">
            <!-- InstanceBeginEditable name="head" -->



<!-- InstanceEndEditable -->
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /></head>
    <body leftmargin="0" bottommargin="0" rightmargin="0" topmargin="0" marginheight="0" marginwidth="0">
   
       

<table width="100%" cellspacing="0" cellpadding="0" border="0">
      <tr valign="top">
      <td colspan="2">              

<table width="100%" cellspacing="0" class="HeadBg" border="0">
      <tr valign="top">
      <td colspan="3" class="SiteLink" bgcolor="darkgray" align="right">
                  <span id="Banner_WelcomeMessage" style="color:#EEEEEE;">Welcome <?php echo $_SESSION[UserName]; ?>! <span class=Accent>|</span></span>
                  
      </td>
      </tr>
      <tr>
      <td width="10" rowspan="2">
      </td>
      <td height="40">
            <span id="Banner_siteName" class="SiteTitle">CAB Intranet</span>
      </td>
      <td align="center">
      </td>
      </tr>
      <tr>
      <td>
<?php include ($_SERVER['DOCUMENT_ROOT'] .'/includes/portal_head.inc'); ?>
      </td>
      </tr>
</table>
      </td>
    </tr>
    <tr>
    <td>
                       
<table width="100%" cellspacing="0" cellpadding="4" border="0">
    <tr height="*" valign="top">
    <td width="5">&nbsp;
    </td>
    <td id="LeftPane" width="170"><div class="level2"><!-- InstanceBeginEditable name="level2" -->
            <?php include ($_SERVER['DOCUMENT_ROOT'] .'/includes/level2_sd.inc'); ?>
          <!-- InstanceEndEditable --></div></td>
      <td width="1">
    </td>
                              <td id="ContentPane" width="*"><table id="_ctl2_t1" cellspacing="0" cellpadding="0">
      <tr valign="top">
            
      </tr>
</table>
<!-- InstanceBeginEditable name="main-content" -->
      <h1>Add Feedlot News Release</h1>
      <form action="subnewsrelease.php" method="post" enctype="multipart/form-data" name="frmSubmit" id= "frmSubmit">
                 
                      <p>
                        <input type="hidden" name="MAX_FILE_SIZE" value="200000" />
                        <input type="hidden" name="action" value="upload" />
                    </p>
                  <p>
                  
                          <input name="btnArticleType" align="right" type="radio" value="BI" />
              Black Ink<br />
                    <input name="btnArticleType" align="right" type="radio" value="CQ" />
                    Corner on Quality<br />
                    <input name="btnArticleType" align="right" type="radio" value="NS" />
                    News Release<br />
                      <input name="btnArticleType" align="right" type="radio" value="PS" />
                    Published Stories<br />
                    <input name="btnArticleType" align="right" type="radio" value="RD" />
                    Radio Release

                  </p>

                  <p>Category:<br />
                  <select name="cboCategory">
                        <option value="NONE" selected></option>
                              <?php
                              while ($cat= mysql_fetch_array($catresult)){;?>
                              <option value="<?php echo $cat['cat_id']?>"><?php echo $cat['category']?></option>
                                <?php
                                      }
                              ?>
                              </select></p>      

        <p> <br />
                        <input type="text" size="50" name="txtTitle" />
      </p>
        <p>Authors: <br />
                        <input type="text" size="50" name="txtAuthor" /> </p>
            <p>Synopsis:<br />
                  <textarea name="txtSynopsis" cols="50"></textarea></p>
                  
            <p>Topic Area:<br />
                  <select name="cboTopicArea">
                  <option value="NONE" selected></option>
                              <?php
                              while ($top= mysql_fetch_array($topresult)){;?>
                              <option value="<?php echo $top['topic_a_id']?>"><?php echo $top['topic_area']?></option>
                                <?php
                                      }
                              ?>
                              </select></p>      
             <p>Topic:<br />
                  <input type="text" size="50" name="txtTopic" /></p>      
   
                <table width="250" border="0" align="right" cellpadding="5" cellspacing="0">
                    <tr>
                      <td><p><strong>REMEMBER:</strong> When you are naming your PDF files, <strong>NO PUNCTUATION or SPACES! </strong></p></td>
                    </tr>
      </table>
                  <p>Browse for press release PDF file: <br />
                  <input type="file"  name="binFile" />
                </p>
                        <p>Browse for press release MP3 file: <br />
                  <input type="file"  name="radioFile" />
                           <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />

                </p>

                  <p>Enter date article  was written:<br />
                    <input type="text" name="txtDate" value="<?PHP echo date("Y-m-d");?>" />
                 
      </p>
                  <p>
                        <input name="Submit" type="submit" value="Submit" />
                </p>
                        <p>
                          <input type="hidden" name="MM_insert" value="frmSubmit" />
      </p>
      </form>
  <!-- InstanceEndEditable --></td>

                                <td id="RightPane" width="230">
         
<!-- InstanceBeginEditable name="right-column" -->l<!-- InstanceEndEditable --><br>

      
    <br></td>

                                <td width="10">&nbsp;
                                   
                              </td>
        </tr>
      </table>
      </td>
  </tr>
    </table>
       
    </body>
<!-- InstanceEnd --></html>
0
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 500 total points
ID: 17167617
Right.

So, $_FILES['binFile'] and $_FILES['radioFile'] are the 2 elements containing details about the uploaded files.

Are the files going to the same place?

If so, create a loop...
<?php

$b_All_OK = True;
$s_working_dir = './uploaded/'; // You will need to create the directory if not already done.
foreach(array('binFile', 'radioFile') as $s_HTML_name)
      {
      // Process $s_HTML_name
      if
            (
            is_uploaded_file($_FILES[$s_HTML_name]['tmp_name']) && 
            move_uploaded_file($_FILES[$s_HTML_name]['tmp_name'], $s_working_dir . $_FILES[$s_HTML_name]['name'])
            )
            {
            // Handle the file.
            $b_All_OK = doSomethingWith($s_HTML_name, $s_working_dir . $_FILES[$s_HTML_name]['name']);
      else
            {
            // Not uploaded or a hack attempt.
            $b_All_OK = False;
            }
      // Don't do anything else if something went wrong.
      if (False === $b_All_OK)
            {
            break;
            }
      }
if (False === $b_All_OK)
      {
      // Something went wrong.
      }

?>
0
 
LVL 2

Author Comment

by:ChadMarsh
ID: 17167870
Nice! That works. Thanks
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question