title: I am trying to upload a file into wordpress but I have different problems
body: <p>I am trying to receive files from a webservice and save them into wordpress but I have following issues,</p>
  
  <ol>
  <li>Some of the files will be uploaded but they are corrupted and I can't open them, although their webservice address is correct.</li>
  <li>It shows the following errors (although it shows these errors but upload the files anyway, which is good :D but I am wondering why I am receiving them).</li>
  </ol>
  
  <p>Notice: Constant ABSPATH already defined in /Applications/MAMP/htdocs/wordpress/wp-load.php on line 22</p>
  
  <p>Fatal error: Call to undefined function wp_generate_attachment_metadata() in /Applications/MAMP/htdocs/wordpress/wp-content/plugins/myproject/Myclasses/retrieve_2334.php on line 95</p>
  
  <p><strong>My Code:</strong></p>
  
  <pre><code>    ini_set( 'display_errors', TRUE );
      error_reporting( E_ALL );
      require("/Applications/MAMP/htdocs/wordpress/wp-load.php");
      $pos = strrpos($Address, "&amp;f=");
      $loc = substr($Address, $pos + 3);
  
      $Address = "http://dev.piction.com/v60/" . $Address;
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_POST, 0);
      curl_setopt($ch, CURLOPT_URL, $Address);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $output = curl_exec($ch);
      curl_close($ch);
  
  
  
      $upload_dir = wp_upload_dir();
      $location= $upload_dir['basedir']. "/" . $loc;
  
  try{
      $location= $upload_dir['path']. "/" . $loc;
      $wp_filetype = wp_check_filetype($loc, null );
      $fullpathfilename = $upload_dir['path'] . "/" . $loc;
  
  
      $fileSaved = file_put_contents($location, $output);
                  if ( !$fileSaved ) {
                      throw new Exception("The file cannot be saved.");
                  }
                  $attachment = array(
                       'post_mime_type' =&gt; $wp_filetype['type'],
                       'post_title' =&gt; preg_replace('/\.[^.]+$/', '', $loc),
                       'post_content' =&gt; '',
                       'post_status' =&gt; 'inherit',
                       'guid' =&gt; $upload_dir['url'] . "/" . $loc
                  );
                  $attach_id = wp_insert_attachment( $attachment, $fullpathfilename, 0 );
                  if ( !$attach_id ) {
                      throw new Exception("Failed to save record into database.");
                  }
                  //require_once(ABSPATH . "wp-admin" . '/includes/image.php');
                  $attach_data = wp_generate_attachment_metadata( $attach_id, $fullpathfilename );
                  wp_update_attachment_metadata( $attach_id,  $attach_data );
  
          } catch (Exception $e) {
                  $error = '&lt;div id="message" class="error"&gt;&lt;p&gt;' . $e-&gt;getMessage() . '&lt;/p&gt;&lt;/div&gt;';
              }
      echo 'Photo is uploaded';
  </code></pre>