Question HTTPWebRequest to php upload

Manuel

New member
Joined
Oct 26, 2011
Messages
1
Programming Experience
Beginner
Hello, I have a script (of internet) to upload images. Use Smarty. I want to know what would be thecode to upload from visual basic. Net (currently use 10.0)

If you do not understand what I mean, I give an example: http://www.vbdotnetforums.com/net-sockets/25413-httpwebrequest-php-upload.html

This is the code (of upload.php):

PD: I leave the files attached if you do not display properly.

Note: This is the file "upload.php" which if you run it with the browser will see a blank page.

PHP:
<?php



include("header.php");



include("include/classes/class.resize.php");


$is_error = 0;
$moved = 0;
$message = '';
$uploaded = false;


if(isset($_POST['task']) && $_POST['task'] == "doUpload")
{
    if(empty($_FILES['image']['name']['0']))
    {
        $flashMessage->setFlash("upload_error", "Please select an image to upload.");
        $redirect = $config['base_url'];
    }
    
    // generate new set id
    $set_id = randString(6);
    
    while(list($key, $value) = each($_FILES['image']['name']))    
    {
        // make sure slot is not empty
        if(!empty($value))
        {
            // set upload directory
            $upload_dir = $config['upload_dir'] . $set_id . "/";
            
            // set file owner
            $owner = ($user->user_exists == 1) ? $user->user_info['id'] : 0;
    
            // get image info
            $file_name = $_FILES['image']['name'][$key];
            $file_tmp_name = $_FILES['image']['tmp_name'][$key];
            $file_size = $_FILES['image']['size'][$key];
            $file_type = strtolower($_FILES['image']['type'][$key]);
            $get_img  = @getimagesize($file_tmp_name);
            $path_info = pathinfo($file_name);
    
            // check file size
            if($file_size >= $config['upload_max_size'])
            {
                $is_error = 1;
                $message = $file_name . " is too big.\n";
            }else
    
            // check file extension
            if(!(in_array(strtolower($path_info['extension']), $config['upload_accepted_ext'])) ||
               !(in_array($file_type, $config['upload_accepted_mime'])))
            {
                $is_error = 1;
                $message = $file_name . " is an invalid extension.\n";
            }else
    
            // no error, continue to upload image
            if($is_error != 1)
            {
                // generate new image name
                $newname = randString(6);
        
                // generate new delete id
                $delete_id = randString(8);
            
                // check if upload dir exists and create it if not
                if(!is_dir($upload_dir))
                {
                    mkdir($upload_dir, 0777);
                }
            
                // move uploaded file to new location
                if(move_uploaded_file($file_tmp_name, $upload_dir . $newname . "." . strtolower($path_info['extension'])))
                {
                    $uploaded = true;
                }
        
                // make thumbs
                if($uploaded)
                {
                    // incriment moved var
                    $moved = 1;
                    
                    // init new class object
                    $resize = new resize($upload_dir . $newname . "." . strtolower($path_info['extension']));
            
                    // make thumb
                    $thumb_name = $newname . "_thumb" . "." . strtolower($path_info['extension']);
                    $resize->resizeImage(350, 350, "auto");
                    $resize->imageSaveQuality(60);
                    $resize->saveImage($upload_dir . $thumb_name);
                
                    // make small thumb
                    $thumb_name_small = $newname . "_thumb_small" . "." . strtolower($path_info['extension']);
                    $resize->resizeImage(120, 120, "exact");
                    $resize->imageSaveQuality(60);
                    $resize->saveImage($upload_dir . $thumb_name_small);
            
                    // is image public?
                    $public = isset($_POST['public']) ? "1" : "0";
            
                   // insert image in to database
                    mysql_query("INSERT INTO uploads (image_name,
                                                      original_name,
                                                      image_type,
                                                      image_size,
                                                      uploaded,
                                                      uploader_ip,
                                                      views,
                                                      last_access,
                                                      delete_code,
                                                      image_public,
                                                      owner,
                                                      set_id
                                                      ) VALUES (
                                                      '".$newname . "." . strtolower($path_info['extension'])."',
                                                      '".$file_name."',
                                                      '".$file_type."',
                                                      '".$file_size."',
                                                      '".time()."',
                                                      '".$_SERVER['REMOTE_ADDR']."',
                                                      '0',
                                                      '".time()."',
                                                      '".$delete_id."',
                                                      '".$public."',
                                                      '".$owner."',
                                                      '".$set_id."')");
                } // if moved
            } // if error
        } // if empty
    } // while
    
    if($moved == 1)
    {
        if($is_error == 1){ $flashMessage->setFlash("upload_error", "Some images were not uploaded due to errors."); }
        $redirect = $config['base_url'] . '/u-' . $set_id;
    }else{
        $flashMessage->setFlash("upload_error", $message);
        $redirect = $config['base_url'];
    }
    die($redirect);
        
} // if action == upload


?>

Archive .tpl:


VB.NET:
{include file='header.tpl'}
<form action="{$config.base_url}/upload.php" id="uploadForm" method="post" enctype="multipart/form-data" onsubmit="return Core.checkUpload();" />
  <div id="content">
    {if $flashMessage->hasFlash('upload_error')}
	 <div style="color:#FF0000;font-weight:bold;text-align:center;">{$flashMessage->getFlash('upload_error')}</div>
	 {/if}
	 {if $flashMessage->hasFlash('ok')}
	 <div style="color:green;font-weight:bold;text-align:center;">{$flashMessage->getFlash('ok')}</div>
	 {/if}
	 {if $flashMessage->hasFlash('notok')}
	 <div style="color:#FF0000;font-weight:bold;text-align:center;">{$flashMessage->getFlash('notok')}</div>
	 {/if}
    <div id="upload-area" class="textbox">
      <div id="file-area">
        <input type="file" name="image[]" id="image" size="58" />
        <div id="fileList"></div>
        <div id="file-message">Choose your images to upload, or upload into an <a href="{$config.base_url}/register.php">account</a>.</div>
      </div>
    </div>
    <div class="left" id="uploadButton">
      <input class="button-big" name="submit" value="Upload" type="submit" />
    </div>
    <div class="clear"></div>
  </div>
  </div>
  </div>
  <input type="hidden" name="task" value="doUpload" />
</form>
{include file='footer.tpl'}
 

Attachments

  • HTTP-Upload.zip
    2.1 KB · Views: 19
Last edited:
Back
Top