SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2012
    Location
    Lancaster,UK
    Posts
    32
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Cannot upload images with PHP

    Hi there,

    I am trying to allow visitors to upload images ( and later want to allow them to add audio files too so it would be nice to get comments on that as well ) .
    All the information gets uploaded correctly except the files. What could be the problem?
    I came up with the following code:

    HTML Code:
    	<form id="add-new-form" enctype="multipart/form-data">
    		<input type="text" name="author" placeholder="author:" />
    		<input type="text" name="title" placeholder="title:" />
    		<textarea rows="10" cols="34" name="content" placeholder="content:"></textarea>
    		<input type="file" name="image" />
    		<input type="submit" id="add-new-button"  value="" />
    	</form>
    PHP Code:
    try {
        
    $conn = new PDO('mysql:host=localhost;dbname=brownie_manualforajump;''username''pass');
        
    $conn->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
    } catch(
    PDOException $e) {
        echo 
    'ERROR: ' $e->getMessage();
    }

    if(isset(
    $_POST['author'])) {
        
    $author htmlspecialchars($_POST['author']);
        
    $title htmlspecialchars($_POST['title']);
        
    $content htmlspecialchars($_POST['content']);

    // properties of the uploaded file
    $name $_FILES["image"]["name"];
    $type $_FILES["image"]["type"];
    $size $_FILES["image"]["size"];
    $temp $_FILES["image"]["tmp_name"];
    $error $_FILES["image"]["error"];

    // if($error > 0) {
    //     die("Error uploading file! Code $error.");
    // } else {
    //     if($type == "video/avi") { // conditions for the file
    //         die("That format is not allowed");
    //     } elseif ($size > 1048576) {
    //         die("File size cannot exceed 1mb");
    //     } else {
            
    move_uploaded_file($temp,"uploaded/".$name);
    //     }
    // }
    // 

        
    $result $author;

        echo 
    json_encode($result);

        try {
            
    $stmt $conn->prepare("INSERT INTO projects VALUES ('', :author, :title, :content, :path)");

            
    $stmt->bindParam('author'$authorPDO::PARAM_STR);
            
    $stmt->bindParam('title'$titlePDO::PARAM_STR);
            
    $stmt->bindParam('content'$contentPDO::PARAM_STR);
            
    $stmt->bindParam('path'$namePDO::PARAM_STR);

            
    $stmt->execute();

        } catch(
    PDOException $e) {
            echo 
    'ERROR: ' $e->getMessage();
        }


  2. #2
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,364
    Mentioned
    80 Post(s)
    Tagged
    3 Thread(s)
    The first thing I would do is echo out these variables and see what they contain:
    PHP Code:
    echo " name = $name <br> Type = $type <br> Size = $size  <br> Temp = $temp  <br> Error = $error "
    Quite often people are either trying to up load a folder rather than a file or the variable contains something else they are not expecting.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2012
    Location
    Lancaster,UK
    Posts
    32
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    That's the problem. All these values are absolutely empty while text input is fine. I have no idea what could be the problem and how to debug this

    It's probably also worth mentioning that I have this code which is executed when 'Submit' button is pressed. It is AJAX so the page does not reload. Maybe that could be the problem

    Code:
    // Make a call to the server to retrieve all data
    function retrieveResults(handleData) {
        return $.ajax({
            url: "forms-processing.php",
            type: 'POST',
            dataType: 'json',
            data: "projects=true",
            success: function(data) {
            	handleData(data);
            }
        });
    }
    
    // Add authors to contributors, menu item
    retrieveResults(function(output){
    	for(var i = 0; i<output.length; i++) {
    		$('<a />', {
    		    href: '#',
    		    text: output[i].title
    		}).wrap('<li />').parent().attr('data-author', output[i].author).appendTo('#project-titles');
    		
    		var position = $("#project-titles li:eq("+i+")").offset(); // calculate offset position of list items in terms of viewport
    		var content = '\
    		<div data-author="' + output[i].author + '" style="left:' + position.left + 'px;top:' + (100 + Math.floor(Math.random()*500)) + 'px;z-index' + i + ';" class="project" id="project' + i + '">\
    			<h2>' + output[i].title + '</h2>\
    			<p>' + output[i].content + '</p>\
    		</div>';
    
    		$('#content').append(content);
    		$('#project' + i).draggable();
    	}
    	
    	var listCount = $("#project-titles li").size();
    	$('.project').on('click', function() {
    		listCount += 1;
    		$(this).css('z-index', listCount);
    	});
    });

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2012
    Location
    Lancaster,UK
    Posts
    32
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ok I figured it out. File upload does not work with AJAX, that is the problem.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •